Skip to content

Commit 76c5dfe

Browse files
sitio-coutolanza
authored andcommitted
[CIR] Add reconcile unrealized casts pass
ghstack-source-id: 8e98e12 Pull Request resolved: #147
1 parent bb11a4c commit 76c5dfe

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,11 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule,
14361436
// emission directly from our frontend.
14371437
pm.addPass(mlir::LLVM::createDIScopeForLLVMFuncOpPass());
14381438

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

14411446
auto result = !mlir::failed(pm.run(theModule));

clang/test/CIR/Lowering/tenary.cir

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: cir-tool %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
1+
// RUN: cir-tool %s -cir-to-llvm -reconcile-unrealized-casts -o - | FileCheck %s -check-prefix=MLIR
22

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

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

26-
// MLIR: module {
27-
// MLIR: llvm.func @_Z1xi(%arg0: i32) -> i32 {
26+
// MLIR: llvm.func @_Z1xi(%arg0: i32) -> i32 {
2827
// MLIR-NEXT: %0 = llvm.mlir.constant(1 : index) : i64
2928
// MLIR-NEXT: %1 = llvm.alloca %0 x i32 {alignment = 4 : i64} : (i64) -> !llvm.ptr
3029
// MLIR-NEXT: %2 = llvm.mlir.constant(1 : index) : i64
@@ -38,17 +37,14 @@ cir.func @_Z1xi(%arg0: !s32i) -> !s32i {
3837
// MLIR-NEXT: llvm.cond_br %8, ^bb1, ^bb2
3938
// MLIR-NEXT: ^bb1: // pred: ^bb0
4039
// MLIR-NEXT: %9 = llvm.mlir.constant(3 : i32) : i32
41-
// MLIR-NEXT: %10 = builtin.unrealized_conversion_cast %9 : i32 to !s32i
4240
// MLIR-NEXT: llvm.br ^bb3(%9 : i32)
4341
// MLIR-NEXT: ^bb2: // pred: ^bb0
44-
// MLIR-NEXT: %11 = llvm.mlir.constant(5 : i32) : i32
45-
// MLIR-NEXT: %12 = builtin.unrealized_conversion_cast %11 : i32 to !s32i
46-
// MLIR-NEXT: llvm.br ^bb3(%11 : i32)
47-
// MLIR-NEXT: ^bb3(%13: i32): // 2 preds: ^bb1, ^bb2
42+
// MLIR-NEXT: %10 = llvm.mlir.constant(5 : i32) : i32
43+
// MLIR-NEXT: llvm.br ^bb3(%10 : i32)
44+
// MLIR-NEXT: ^bb3(%11: i32): // 2 preds: ^bb1, ^bb2
4845
// MLIR-NEXT: llvm.br ^bb4
4946
// MLIR-NEXT: ^bb4: // pred: ^bb3
50-
// MLIR-NEXT: llvm.store %13, %3 : i32, !llvm.ptr
51-
// MLIR-NEXT: %14 = llvm.load %3 : !llvm.ptr
52-
// MLIR-NEXT: llvm.return %14 : i32
53-
// MLIR-NEXT: }
54-
// MLIR-NEXT: }
47+
// MLIR-NEXT: llvm.store %11, %3 : i32, !llvm.ptr
48+
// MLIR-NEXT: %12 = llvm.load %3 : !llvm.ptr
49+
// MLIR-NEXT: llvm.return %12 : i32
50+
// MLIR-NEXT: }

clang/tools/cir-tool/cir-tool.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
1516
#include "mlir/Dialect/Arith/IR/Arith.h"
1617
#include "mlir/Dialect/DLTI/DLTI.h"
1718
#include "mlir/Dialect/Func/IR/FuncOps.h"
@@ -46,6 +47,10 @@ int main(int argc, char **argv) {
4647
return cir::direct::createConvertCIRToLLVMPass();
4748
});
4849

50+
::mlir::registerPass([]() -> std::unique_ptr<::mlir::Pass> {
51+
return mlir::createReconcileUnrealizedCastsPass();
52+
});
53+
4954
mlir::registerTransformsPasses();
5055

5156
return failed(MlirOptMain(

0 commit comments

Comments
 (0)