Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,11 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule,
pm.addNestedPass<mlir::LLVM::LLVMFuncOp>(
mlir::LLVM::createDIScopeForLLVMFuncOpPass());

// FIXME(cir): this shouldn't be necessary. It's meant to be a temporary
// workaround until we understand why some unrealized casts are being emmited
// and how to properly avoid them.
pm.addPass(mlir::createReconcileUnrealizedCastsPass());

(void)mlir::applyPassManagerCLOptions(pm);

auto result = !mlir::failed(pm.run(theModule));
Expand Down
22 changes: 9 additions & 13 deletions clang/test/CIR/Lowering/tenary.cir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: cir-tool %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-tool %s -cir-to-llvm -reconcile-unrealized-casts -o - | FileCheck %s -check-prefix=MLIR

!s32i = !cir.int<s, 32>

Expand All @@ -23,8 +23,7 @@ cir.func @_Z1xi(%arg0: !s32i) -> !s32i {
}
}

// MLIR: module {
// MLIR: llvm.func @_Z1xi(%arg0: i32) -> i32 {
// MLIR: llvm.func @_Z1xi(%arg0: i32) -> i32 {
// MLIR-NEXT: %0 = llvm.mlir.constant(1 : index) : i64
// MLIR-NEXT: %1 = llvm.alloca %0 x i32 {alignment = 4 : i64} : (i64) -> !llvm.ptr<i32>
// MLIR-NEXT: %2 = llvm.mlir.constant(1 : index) : i64
Expand All @@ -38,17 +37,14 @@ cir.func @_Z1xi(%arg0: !s32i) -> !s32i {
// MLIR-NEXT: llvm.cond_br %8, ^bb1, ^bb2
// MLIR-NEXT: ^bb1: // pred: ^bb0
// MLIR-NEXT: %9 = llvm.mlir.constant(3 : i32) : i32
// MLIR-NEXT: %10 = builtin.unrealized_conversion_cast %9 : i32 to !s32i
// MLIR-NEXT: llvm.br ^bb3(%9 : i32)
// MLIR-NEXT: ^bb2: // pred: ^bb0
// MLIR-NEXT: %11 = llvm.mlir.constant(5 : i32) : i32
// MLIR-NEXT: %12 = builtin.unrealized_conversion_cast %11 : i32 to !s32i
// MLIR-NEXT: llvm.br ^bb3(%11 : i32)
// MLIR-NEXT: ^bb3(%13: i32): // 2 preds: ^bb1, ^bb2
// MLIR-NEXT: %10 = llvm.mlir.constant(5 : i32) : i32
// MLIR-NEXT: llvm.br ^bb3(%10 : i32)
// MLIR-NEXT: ^bb3(%11: i32): // 2 preds: ^bb1, ^bb2
// MLIR-NEXT: llvm.br ^bb4
// MLIR-NEXT: ^bb4: // pred: ^bb3
// MLIR-NEXT: llvm.store %13, %3 : !llvm.ptr<i32>
// MLIR-NEXT: %14 = llvm.load %3 : !llvm.ptr<i32>
// MLIR-NEXT: llvm.return %14 : i32
// MLIR-NEXT: }
// MLIR-NEXT: }
// MLIR-NEXT: llvm.store %11, %3 : !llvm.ptr<i32>
// MLIR-NEXT: %12 = llvm.load %3 : !llvm.ptr<i32>
// MLIR-NEXT: llvm.return %12 : i32
// MLIR-NEXT: }
5 changes: 5 additions & 0 deletions clang/tools/cir-tool/cir-tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//

#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
Expand Down Expand Up @@ -46,6 +47,10 @@ int main(int argc, char **argv) {
return cir::direct::createConvertCIRToLLVMPass();
});

::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
return mlir::createReconcileUnrealizedCastsPass();
});

mlir::registerTransformsPasses();

return failed(MlirOptMain(
Expand Down