Skip to content

Commit

Permalink
[mlir] Enable opaque pointers in LLVM conversion passes by default
Browse files Browse the repository at this point in the history
Part of https://discourse.llvm.org/t/rfc-switching-the-llvm-dialect-and-dialect-lowerings-to-opaque-pointers/68179

When this patch lands any downstream users with custom LLVM conversion passes not yet using opaque pointers will start either experiencing assertions being triggered, null pointer dereferences or at the very least verifier errors. These can be either fixed by switching to opaque pointers or simply disabling opaque pointers in both pass options of any upstream conversion passes and any uses of `LLVMTypeConverter` via the `LowerToLLVMOptions`.

Users using just MLIRs conversion passes to the LLVM Dialect should not experience any change in functionality except when inspecting the output from the passes.

Differential Revision: https://reviews.llvm.org/D145585
  • Loading branch information
zero9178 committed Mar 10, 2023
1 parent 7163b1a commit 552522b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
8 changes: 7 additions & 1 deletion flang/lib/Optimizer/CodeGen/TypeConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ namespace fir {
class LLVMTypeConverter : public mlir::LLVMTypeConverter {
public:
LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA)
: mlir::LLVMTypeConverter(module.getContext()),
: mlir::LLVMTypeConverter(module.getContext(),
[&] {
mlir::LowerToLLVMOptions options(
module.getContext());
options.useOpaquePointers = false;
return options;
}()),
kindMapping(getKindMapping(module)),
specifics(CodeGenSpecifics::get(module.getContext(),
getTargetTriple(module),
Expand Down
2 changes: 1 addition & 1 deletion mlir/include/mlir/Conversion/LLVMCommon/LoweringOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LowerToLLVMOptions {
LowerToLLVMOptions(MLIRContext *ctx, const DataLayout &dl);

bool useBarePtrCallConv = false;
bool useOpaquePointers = false;
bool useOpaquePointers = true;

enum class AllocLowering {
/// Use malloc for for heap allocations.
Expand Down
33 changes: 16 additions & 17 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def ConvertAsyncToLLVMPass : Pass<"convert-async-to-llvm", "ModuleOp"> {
];
let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];
}
Expand Down Expand Up @@ -251,7 +251,7 @@ def ConvertControlFlowToLLVMPass : Pass<"convert-cf-to-llvm", "ModuleOp"> {
/*default=kDeriveIndexBitwidthFromDataLayout*/"0",
"Bitwidth of the index type, 0 to use size of machine word">,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];
}
Expand Down Expand Up @@ -314,7 +314,7 @@ def ConvertFuncToLLVMPass : Pass<"convert-func-to-llvm", "ModuleOp"> {
"String description (LLVM format) of the data layout that is "
"expected on the produced module">,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];
}
Expand Down Expand Up @@ -362,7 +362,7 @@ def GpuToLLVMConversionPass : Pass<"gpu-to-llvm", "ModuleOp"> {
"Annotation attribute string for GPU binary"
>,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];

Expand All @@ -388,7 +388,7 @@ def LowerHostCodeToLLVMPass : Pass<"lower-host-to-llvm", "ModuleOp"> {

let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];

Expand All @@ -414,7 +414,7 @@ def ConvertGpuOpsToNVVMOps : Pass<"convert-gpu-to-nvvm", "gpu::GPUModuleOp"> {
Option<"hasRedux", "has-redux", "bool", /*default=*/"false",
"Target gpu supports redux">,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];
}
Expand Down Expand Up @@ -451,7 +451,7 @@ def ConvertGpuOpsToROCDLOps : Pass<"convert-gpu-to-rocdl", "gpu::GPUModuleOp"> {
clEnumValN(::mlir::gpu::amd::Runtime::OpenCL, "OpenCL", "OpenCL")
)}]>,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];
}
Expand Down Expand Up @@ -507,7 +507,7 @@ def ConvertVulkanLaunchFuncToVulkanCallsPass

let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];

Expand Down Expand Up @@ -545,11 +545,10 @@ def ConvertLinalgToLLVMPass : Pass<"convert-linalg-to-llvm", "ModuleOp"> {
let summary = "Convert the operations from the linalg dialect into the LLVM "
"dialect";
let dependentDialects = ["scf::SCFDialect", "LLVM::LLVMDialect"];

let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];
}

Expand Down Expand Up @@ -657,7 +656,7 @@ def FinalizeMemRefToLLVMConversionPass :
"Use generic allocation and deallocation functions instead of the "
"classic 'malloc', 'aligned_alloc' and 'free' functions">,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];
}
Expand Down Expand Up @@ -702,7 +701,7 @@ def ConvertNVGPUToNVVMPass : Pass<"convert-nvgpu-to-nvvm"> {
];
let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];
}
Expand All @@ -727,7 +726,7 @@ def ConvertOpenACCToLLVMPass : Pass<"convert-openacc-to-llvm", "ModuleOp"> {
let dependentDialects = ["LLVM::LLVMDialect"];
let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">,
];
}
Expand Down Expand Up @@ -798,7 +797,7 @@ def ConvertSCFToOpenMPPass : Pass<"convert-scf-to-openmp", "ModuleOp"> {

let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];

Expand Down Expand Up @@ -888,7 +887,7 @@ def ConvertSPIRVToLLVMPass : Pass<"convert-spirv-to-llvm", "ModuleOp"> {

let options = [
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];
}
Expand Down Expand Up @@ -1098,7 +1097,7 @@ def ConvertVectorToLLVMPass : Pass<"convert-vector-to-llvm", "ModuleOp"> {
"Enables the use of X86Vector dialect while lowering the vector "
"dialect.">,
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
/*default=*/"false", "Generate LLVM IR using opaque pointers "
/*default=*/"true", "Generate LLVM IR using opaque pointers "
"instead of typed pointers">
];
}
Expand Down
2 changes: 0 additions & 2 deletions mlir/test/mlir-cpu-runner/bare-ptr-call-conv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func.func @simple_add1_add2_test(%arg0: memref<2xf32>, %arg1: memref<2xf32>) {
}

// External declarations.
llvm.func @malloc(i64) -> !llvm.ptr<i8>
llvm.func @free(!llvm.ptr<i8>)
func.func private @printF32(%arg0: f32)
func.func private @printComma()
func.func private @printNewline()
Expand Down

0 comments on commit 552522b

Please sign in to comment.