Do not run `cf-to-llvm` as part of `func-to-llvm`. This commit fixes https://github.com/llvm/llvm-project/issues/70982. This commit changes the way how `func.func` ops are lowered to LLVM. Previously, the signature of the entire region (i.e., entry block and all other blocks in the `func.func` op) was converted as part of the `func.func` lowering pattern. Now, only the entry block is converted. The remaining block signatures are converted together with `cf.br` and `cf.cond_br` as part of `cf-to-llvm`. All unstructured control flow is not converted as part of a single pass (`cf-to-llvm`). `func-to-llvm` no longer deals with unstructured control flow. Also add more test cases for control flow dialect ops. Note: This PR is in preparation of #120431, which adds an additional GPU-specific lowering for `cf.assert`. This was a problem because `cf.assert` used to be converted as part of `func-to-llvm`. Note for LLVM integration: If you see failures, add `-convert-cf-to-llvm` to your pass pipeline.
89 lines
3.8 KiB
C++
89 lines
3.8 KiB
C++
//===- mlir-vulkan-runner.cpp - MLIR Vulkan Execution Driver --------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This is a command line utility that executes an MLIR file on the Vulkan by
|
|
// translating MLIR GPU module to SPIR-V and host part to LLVM IR before
|
|
// JIT-compiling and executing the latter.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
|
|
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
|
|
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
|
|
#include "mlir/Conversion/GPUToVulkan/ConvertGPUToVulkanPass.h"
|
|
#include "mlir/Conversion/LLVMCommon/LoweringOptions.h"
|
|
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
|
|
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
|
|
#include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVMPass.h"
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
|
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
|
|
#include "mlir/Dialect/GPU/Transforms/Passes.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
#include "mlir/Dialect/LLVMIR/Transforms/RequestCWrappers.h"
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
|
#include "mlir/Dialect/MemRef/Transforms/Passes.h"
|
|
#include "mlir/Dialect/SCF/IR/SCF.h"
|
|
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
|
|
#include "mlir/Dialect/Vector/IR/VectorOps.h"
|
|
#include "mlir/ExecutionEngine/JitRunner.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Pass/PassManager.h"
|
|
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
|
|
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
|
|
#include "llvm/Support/InitLLVM.h"
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
using namespace mlir;
|
|
|
|
static LogicalResult runMLIRPasses(Operation *op, JitRunnerOptions &) {
|
|
auto module = dyn_cast<ModuleOp>(op);
|
|
if (!module)
|
|
return op->emitOpError("expected a 'builtin.module' op");
|
|
PassManager passManager(module.getContext());
|
|
if (failed(applyPassManagerCLOptions(passManager)))
|
|
return failure();
|
|
|
|
passManager.addPass(createConvertGpuLaunchFuncToVulkanLaunchFuncPass());
|
|
passManager.addPass(createFinalizeMemRefToLLVMConversionPass());
|
|
passManager.addPass(createConvertVectorToLLVMPass());
|
|
passManager.nest<func::FuncOp>().addPass(LLVM::createRequestCWrappersPass());
|
|
ConvertFuncToLLVMPassOptions funcToLLVMOptions{};
|
|
funcToLLVMOptions.indexBitwidth =
|
|
DataLayout(module).getTypeSizeInBits(IndexType::get(module.getContext()));
|
|
passManager.addPass(createConvertFuncToLLVMPass(funcToLLVMOptions));
|
|
passManager.addPass(createArithToLLVMConversionPass());
|
|
passManager.addPass(createConvertControlFlowToLLVMPass());
|
|
passManager.addPass(createReconcileUnrealizedCastsPass());
|
|
passManager.addPass(createConvertVulkanLaunchFuncToVulkanCallsPass());
|
|
|
|
return passManager.run(module);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
llvm::llvm_shutdown_obj x;
|
|
registerPassManagerCLOptions();
|
|
|
|
llvm::InitLLVM y(argc, argv);
|
|
llvm::InitializeNativeTarget();
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
|
|
|
mlir::JitRunnerConfig jitRunnerConfig;
|
|
jitRunnerConfig.mlirTransformer = runMLIRPasses;
|
|
|
|
mlir::DialectRegistry registry;
|
|
registry.insert<mlir::arith::ArithDialect, mlir::LLVM::LLVMDialect,
|
|
mlir::gpu::GPUDialect, mlir::spirv::SPIRVDialect,
|
|
mlir::scf::SCFDialect, mlir::func::FuncDialect,
|
|
mlir::memref::MemRefDialect, mlir::vector::VectorDialect>();
|
|
mlir::registerBuiltinDialectTranslation(registry);
|
|
mlir::registerLLVMDialectTranslation(registry);
|
|
|
|
return mlir::JitRunnerMain(argc, argv, registry, jitRunnerConfig);
|
|
}
|