Skip to content

Commit cbd7aaa

Browse files
committed
[mlir][cf] Add support for opaque pointers to ControlFlowToLLVM lowering
Part of https://discourse.llvm.org/t/rfc-switching-the-llvm-dialect-and-dialect-lowerings-to-opaque-pointers/68179 This is a very simple patch since there is only one use of pointers types in `cf.assert` that has to be changed. Pointer types are conditionally created with element types and the GEP had to be adjusted to use the array type as base type. Differential Revision: https://reviews.llvm.org/D143583
1 parent 785009e commit cbd7aaa

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

mlir/include/mlir/Conversion/Passes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ def ConvertControlFlowToLLVM : Pass<"convert-cf-to-llvm", "ModuleOp"> {
248248
Option<"indexBitwidth", "index-bitwidth", "unsigned",
249249
/*default=kDeriveIndexBitwidthFromDataLayout*/"0",
250250
"Bitwidth of the index type, 0 to use size of machine word">,
251+
Option<"useOpaquePointers", "use-opaque-pointers", "bool",
252+
/*default=*/"false", "Generate LLVM IR using opaque pointers "
253+
"instead of typed pointers">,
251254
];
252255
}
253256

mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static std::string generateGlobalMsgSymbolName(ModuleOp moduleOp) {
4545

4646
/// Generate IR that prints the given string to stderr.
4747
static void createPrintMsg(OpBuilder &builder, Location loc, ModuleOp moduleOp,
48-
StringRef msg) {
48+
StringRef msg, LLVMTypeConverter &typeConverter) {
4949
auto ip = builder.saveInsertionPoint();
5050
builder.setInsertionPointToStart(moduleOp.getBody());
5151
MLIRContext *ctx = builder.getContext();
@@ -68,12 +68,13 @@ static void createPrintMsg(OpBuilder &builder, Location loc, ModuleOp moduleOp,
6868
// Emit call to `printStr` in runtime library.
6969
builder.restoreInsertionPoint(ip);
7070
auto msgAddr = builder.create<LLVM::AddressOfOp>(
71-
loc, LLVM::LLVMPointerType::get(arrayTy), globalOp.getName());
71+
loc, typeConverter.getPointerType(arrayTy), globalOp.getName());
7272
SmallVector<LLVM::GEPArg> indices(1, 0);
7373
Value gep = builder.create<LLVM::GEPOp>(
74-
loc, LLVM::LLVMPointerType::get(builder.getI8Type()), msgAddr, indices);
75-
Operation *printer =
76-
LLVM::lookupOrCreatePrintStrFn(moduleOp, /*TODO: opaquePointers=*/false);
74+
loc, typeConverter.getPointerType(builder.getI8Type()), arrayTy, msgAddr,
75+
indices);
76+
Operation *printer = LLVM::lookupOrCreatePrintStrFn(
77+
moduleOp, typeConverter.useOpaquePointers());
7778
builder.create<LLVM::CallOp>(loc, TypeRange(), SymbolRefAttr::get(printer),
7879
gep);
7980
}
@@ -102,7 +103,7 @@ struct AssertOpLowering : public ConvertOpToLLVMPattern<cf::AssertOp> {
102103

103104
// Failed block: Generate IR to print the message and call `abort`.
104105
Block *failureBlock = rewriter.createBlock(opBlock->getParent());
105-
createPrintMsg(rewriter, loc, module, op.getMsg());
106+
createPrintMsg(rewriter, loc, module, op.getMsg(), *getTypeConverter());
106107
if (abortOnFailedAssert) {
107108
// Insert the `abort` declaration if necessary.
108109
auto abortFunc = module.lookupSymbol<LLVM::LLVMFuncOp>("abort");
@@ -274,6 +275,7 @@ struct ConvertControlFlowToLLVM
274275
LowerToLLVMOptions options(&getContext());
275276
if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
276277
options.overrideIndexBitwidth(indexBitwidth);
278+
options.useOpaquePointers = useOpaquePointers;
277279

278280
LLVMTypeConverter converter(&getContext(), options);
279281
mlir::cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: mlir-opt %s -convert-cf-to-llvm='use-opaque-pointers=1' | FileCheck %s
2+
3+
func.func @main() {
4+
%a = arith.constant 0 : i1
5+
cf.assert %a, "assertion foo"
6+
return
7+
}
8+
9+
// CHECK: llvm.func @puts(!llvm.ptr)
10+
11+
// CHECK-LABEL: @main
12+
// CHECK: llvm.cond_br %{{.*}}, ^{{.*}}, ^[[FALSE_BRANCH:[[:alnum:]]+]]
13+
14+
// CHECK: ^[[FALSE_BRANCH]]:
15+
// CHECK: %[[ADDRESS_OF:.*]] = llvm.mlir.addressof @{{.*}} : !llvm.ptr{{$}}
16+
// CHECK: %[[GEP:.*]] = llvm.getelementptr %[[ADDRESS_OF]][0] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<{{[0-9]+}} x i8>
17+
// CHECK: llvm.call @puts(%[[GEP]]) : (!llvm.ptr) -> ()

0 commit comments

Comments
 (0)