Skip to content

Commit

Permalink
[CIR][Lowering] use cir.int type in LIT tests (#266)
Browse files Browse the repository at this point in the history
Here is the promised patch that adds proper type conversion to CIR ->
MLIR conversion. I tried to keep the changes minimum but the existing
implementation doesn't use `TypeConverter`.

This should not have any functional changes except for one tiny fix that
registers the `cf` dialect, which should allow `goto.mlir` to pass.
Happy to break the PR into two if requested.
  • Loading branch information
Kuree authored and lanza committed Apr 29, 2024
1 parent bc24a71 commit f48cd43
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 270 deletions.
316 changes: 163 additions & 153 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions clang/test/CIR/Lowering/ThroughMLIR/array.cir
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

module {
cir.func @foo() {
Expand All @@ -10,15 +9,7 @@ module {

// MLIR: module {
// MLIR-NEXT: func @foo() {
// MLIR-NEXT: = memref.alloca() {alignment = 16 : i64} : memref<10xi32>
// MLIR-NEXT: = memref.alloca() {alignment = 16 : i64} : memref<memref<10xi32>>
// MLIR-NEXT: return
// MLIR-NEXT: }
// MLIR-NEXT: }

// LLVM: = alloca i32, i64 10, align 16
// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } undef, ptr %1, 0
// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %2, ptr %1, 1
// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %3, i64 0, 2
// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %4, i64 10, 3, 0
// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %5, i64 1, 4, 0
// LLVM-NEXT: ret void
91 changes: 46 additions & 45 deletions clang/test/CIR/Lowering/ThroughMLIR/binop-unsigned-int.cir
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM
!u32i = !cir.int<u, 32>

module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca i32, cir.ptr <i32>, ["b", init] {alignment = 4 : i64}
%2 = cir.alloca i32, cir.ptr <i32>, ["x", init] {alignment = 4 : i64}
%3 = cir.const(2 : i32) : i32 cir.store %3, %0 : i32, cir.ptr <i32>
%4 = cir.const(1 : i32) : i32 cir.store %4, %1 : i32, cir.ptr <i32>
%5 = cir.load %0 : cir.ptr <i32>, i32
%6 = cir.load %1 : cir.ptr <i32>, i32
%7 = cir.binop(mul, %5, %6) : i32
cir.store %7, %2 : i32, cir.ptr <i32>
%8 = cir.load %2 : cir.ptr <i32>, i32
%9 = cir.load %1 : cir.ptr <i32>, i32
%10 = cir.binop(div, %8, %9) : i32
cir.store %10, %2 : i32, cir.ptr <i32>
%11 = cir.load %2 : cir.ptr <i32>, i32
%12 = cir.load %1 : cir.ptr <i32>, i32
%13 = cir.binop(rem, %11, %12) : i32
cir.store %13, %2 : i32, cir.ptr <i32>
%14 = cir.load %2 : cir.ptr <i32>, i32
%15 = cir.load %1 : cir.ptr <i32>, i32
%16 = cir.binop(add, %14, %15) : i32
cir.store %16, %2 : i32, cir.ptr <i32>
%17 = cir.load %2 : cir.ptr <i32>, i32
%18 = cir.load %1 : cir.ptr <i32>, i32
%19 = cir.binop(sub, %17, %18) : i32
cir.store %19, %2 : i32, cir.ptr <i32>
%0 = cir.alloca !u32i, cir.ptr <!u32i>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca !u32i, cir.ptr <!u32i>, ["b", init] {alignment = 4 : i64}
%2 = cir.alloca !u32i, cir.ptr <!u32i>, ["x", init] {alignment = 4 : i64}
%3 = cir.const(#cir.int<2> : !u32i) : !u32i cir.store %3, %0 : !u32i, cir.ptr <!u32i>
%4 = cir.const(#cir.int<1> : !u32i) : !u32i cir.store %4, %1 : !u32i, cir.ptr <!u32i>
%5 = cir.load %0 : cir.ptr <!u32i>, !u32i
%6 = cir.load %1 : cir.ptr <!u32i>, !u32i
%7 = cir.binop(mul, %5, %6) : !u32i
cir.store %7, %2 : !u32i, cir.ptr <!u32i>
%8 = cir.load %2 : cir.ptr <!u32i>, !u32i
%9 = cir.load %1 : cir.ptr <!u32i>, !u32i
%10 = cir.binop(div, %8, %9) : !u32i
cir.store %10, %2 : !u32i, cir.ptr <!u32i>
%11 = cir.load %2 : cir.ptr <!u32i>, !u32i
%12 = cir.load %1 : cir.ptr <!u32i>, !u32i
%13 = cir.binop(rem, %11, %12) : !u32i
cir.store %13, %2 : !u32i, cir.ptr <!u32i>
%14 = cir.load %2 : cir.ptr <!u32i>, !u32i
%15 = cir.load %1 : cir.ptr <!u32i>, !u32i
%16 = cir.binop(add, %14, %15) : !u32i
cir.store %16, %2 : !u32i, cir.ptr <!u32i>
%17 = cir.load %2 : cir.ptr <!u32i>, !u32i
%18 = cir.load %1 : cir.ptr <!u32i>, !u32i
%19 = cir.binop(sub, %17, %18) : !u32i
cir.store %19, %2 : !u32i, cir.ptr <!u32i>
// should move to cir.shift, which only accepts
// CIR types.
// %20 = cir.load %2 : cir.ptr <i32>, i32
// %21 = cir.load %1 : cir.ptr <i32>, i32
// %22 = cir.binop(shr, %20, %21) : i32
// cir.store %22, %2 : i32, cir.ptr <i32>
// %23 = cir.load %2 : cir.ptr <i32>, i32
// %24 = cir.load %1 : cir.ptr <i32>, i32
// %25 = cir.binop(shl, %23, %24) : i32
// cir.store %25, %2 : i32, cir.ptr <i32>
%26 = cir.load %2 : cir.ptr <i32>, i32
%27 = cir.load %1 : cir.ptr <i32>, i32
%28 = cir.binop(and, %26, %27) : i32
cir.store %28, %2 : i32, cir.ptr <i32>
%29 = cir.load %2 : cir.ptr <i32>, i32
%30 = cir.load %1 : cir.ptr <i32>, i32
%31 = cir.binop(xor, %29, %30) : i32
cir.store %31, %2 : i32, cir.ptr <i32>
%32 = cir.load %2 : cir.ptr <i32>, i32
%33 = cir.load %1 : cir.ptr <i32>, i32
%34 = cir.binop(or, %32, %33) : i32
cir.store %34, %2 : i32, cir.ptr <i32>
// %20 = cir.load %2 : cir.ptr <!u32i>, !u32i
// %21 = cir.load %1 : cir.ptr <!u32i>, !u32i
// %22 = cir.binop(shr, %20, %21) : !u32i
// cir.store %22, %2 : !u32i, cir.ptr <!u32i>
// %23 = cir.load %2 : cir.ptr <!u32i>, !u32i
// %24 = cir.load %1 : cir.ptr <!u32i>, !u32i
// %25 = cir.binop(shl, %23, %24) : !u32i
// cir.store %25, %2 : !u32i, cir.ptr <!u32i>
%26 = cir.load %2 : cir.ptr <!u32i>, !u32i
%27 = cir.load %1 : cir.ptr <!u32i>, !u32i
%28 = cir.binop(and, %26, %27) : !u32i
cir.store %28, %2 : !u32i, cir.ptr <!u32i>
%29 = cir.load %2 : cir.ptr <!u32i>, !u32i
%30 = cir.load %1 : cir.ptr <!u32i>, !u32i
%31 = cir.binop(xor, %29, %30) : !u32i
cir.store %31, %2 : !u32i, cir.ptr <!u32i>
%32 = cir.load %2 : cir.ptr <!u32i>, !u32i
%33 = cir.load %1 : cir.ptr <!u32i>, !u32i
%34 = cir.binop(or, %32, %33) : !u32i
cir.store %34, %2 : !u32i, cir.ptr <!u32i>
cir.return
}
}
Expand Down
41 changes: 21 additions & 20 deletions clang/test/CIR/Lowering/ThroughMLIR/cmp.cir
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!s32i = !cir.int<s, 32>
module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["a"] {alignment = 4 : i64}
%1 = cir.alloca i32, cir.ptr <i32>, ["b"] {alignment = 4 : i64}
%0 = cir.alloca !s32i, cir.ptr <!s32i>, ["a"] {alignment = 4 : i64}
%1 = cir.alloca !s32i, cir.ptr <!s32i>, ["b"] {alignment = 4 : i64}
%2 = cir.alloca f32, cir.ptr <f32>, ["c"] {alignment = 4 : i64}
%3 = cir.alloca f32, cir.ptr <f32>, ["d"] {alignment = 4 : i64}
%4 = cir.alloca !cir.bool, cir.ptr <!cir.bool>, ["e"] {alignment = 1 : i64}
%5 = cir.load %0 : cir.ptr <i32>, i32
%6 = cir.load %1 : cir.ptr <i32>, i32
%7 = cir.cmp(gt, %5, %6) : i32, !cir.bool
%8 = cir.load %0 : cir.ptr <i32>, i32
%9 = cir.load %1 : cir.ptr <i32>, i32
%10 = cir.cmp(eq, %8, %9) : i32, !cir.bool
%11 = cir.load %0 : cir.ptr <i32>, i32
%12 = cir.load %1 : cir.ptr <i32>, i32
%13 = cir.cmp(lt, %11, %12) : i32, !cir.bool
%14 = cir.load %0 : cir.ptr <i32>, i32
%15 = cir.load %1 : cir.ptr <i32>, i32
%16 = cir.cmp(ge, %14, %15) : i32, !cir.bool
%17 = cir.load %0 : cir.ptr <i32>, i32
%18 = cir.load %1 : cir.ptr <i32>, i32
%19 = cir.cmp(ne, %17, %18) : i32, !cir.bool
%20 = cir.load %0 : cir.ptr <i32>, i32
%21 = cir.load %1 : cir.ptr <i32>, i32
%22 = cir.cmp(le, %20, %21) : i32, !cir.bool
%5 = cir.load %0 : cir.ptr <!s32i>, !s32i
%6 = cir.load %1 : cir.ptr <!s32i>, !s32i
%7 = cir.cmp(gt, %5, %6) : !s32i, !cir.bool
%8 = cir.load %0 : cir.ptr <!s32i>, !s32i
%9 = cir.load %1 : cir.ptr <!s32i>, !s32i
%10 = cir.cmp(eq, %8, %9) : !s32i, !cir.bool
%11 = cir.load %0 : cir.ptr <!s32i>, !s32i
%12 = cir.load %1 : cir.ptr <!s32i>, !s32i
%13 = cir.cmp(lt, %11, %12) : !s32i, !cir.bool
%14 = cir.load %0 : cir.ptr <!s32i>, !s32i
%15 = cir.load %1 : cir.ptr <!s32i>, !s32i
%16 = cir.cmp(ge, %14, %15) : !s32i, !cir.bool
%17 = cir.load %0 : cir.ptr <!s32i>, !s32i
%18 = cir.load %1 : cir.ptr <!s32i>, !s32i
%19 = cir.cmp(ne, %17, %18) : !s32i, !cir.bool
%20 = cir.load %0 : cir.ptr <!s32i>, !s32i
%21 = cir.load %1 : cir.ptr <!s32i>, !s32i
%22 = cir.cmp(le, %20, %21) : !s32i, !cir.bool
%23 = cir.load %2 : cir.ptr <f32>, f32
%24 = cir.load %3 : cir.ptr <f32>, f32
%25 = cir.cmp(gt, %23, %24) : f32, !cir.bool
Expand Down
23 changes: 12 additions & 11 deletions clang/test/CIR/Lowering/ThroughMLIR/goto.cir
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// RUN: cir-opt %s -canonicalize -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -canonicalize -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!u32i = !cir.int<u, 32>
module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["b", init] {alignment = 4 : i64}
%1 = cir.const(1 : i32) : i32
cir.store %1, %0 : i32, cir.ptr <i32>
%0 = cir.alloca !u32i, cir.ptr <!u32i>, ["b", init] {alignment = 4 : i64}
%1 = cir.const(#cir.int<1> : !u32i) : !u32i
cir.store %1, %0 : !u32i, cir.ptr <!u32i>
cir.br ^bb2
^bb1: // no predecessors
%2 = cir.load %0 : cir.ptr <i32>, i32
%3 = cir.const(1 : i32) : i32
%4 = cir.binop(add, %2, %3) : i32
cir.store %4, %0 : i32, cir.ptr <i32>
%2 = cir.load %0 : cir.ptr <!u32i>, !u32i
%3 = cir.const(#cir.int<1> : !u32i) : !u32i
%4 = cir.binop(add, %2, %3) : !u32i
cir.store %4, %0 : !u32i, cir.ptr <!u32i>
cir.br ^bb2
^bb2: // 2 preds: ^bb0, ^bb1
%5 = cir.load %0 : cir.ptr <i32>, i32
%6 = cir.const(2 : i32) : i32
%7 = cir.binop(add, %5, %6) : i32
cir.store %7, %0 : i32, cir.ptr <i32>
%5 = cir.load %0 : cir.ptr <!u32i>, !u32i
%6 = cir.const(#cir.int<2> : !u32i) : !u32i
%7 = cir.binop(add, %5, %6) : !u32i
cir.store %7, %0 : !u32i, cir.ptr <!u32i>
cir.return
}
}
Expand Down
13 changes: 7 additions & 6 deletions clang/test/CIR/Lowering/ThroughMLIR/memref.cir
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!u32i = !cir.int<u, 32>
module {
cir.func @foo() -> i32 {
%0 = cir.alloca i32, cir.ptr <i32>, ["x", init] {alignment = 4 : i64}
%1 = cir.const(1 : i32) : i32
cir.store %1, %0 : i32, cir.ptr <i32>
%2 = cir.load %0 : cir.ptr <i32>, i32
cir.return %2 : i32
cir.func @foo() -> !u32i {
%0 = cir.alloca !u32i, cir.ptr <!u32i>, ["x", init] {alignment = 4 : i64}
%1 = cir.const(#cir.int<1> : !u32i) : !u32i
cir.store %1, %0 : !u32i, cir.ptr <!u32i>
%2 = cir.load %0 : cir.ptr <!u32i>, !u32i
cir.return %2 : !u32i
}
}

Expand Down
7 changes: 4 additions & 3 deletions clang/test/CIR/Lowering/ThroughMLIR/scope.cir
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!u32i = !cir.int<u, 32>
module {
cir.func @foo() {
cir.scope {
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
%1 = cir.const(4 : i32) : i32
cir.store %1, %0 : i32, cir.ptr <i32>
%0 = cir.alloca !u32i, cir.ptr <!u32i>, ["a", init] {alignment = 4 : i64}
%1 = cir.const(#cir.int<4> : !u32i) : !u32i
cir.store %1, %0 : !u32i, cir.ptr <!u32i>
}
cir.return
}
Expand Down
23 changes: 12 additions & 11 deletions clang/test/CIR/Lowering/ThroughMLIR/unary-inc-dec.cir
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!s32i = !cir.int<s, 32>
module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca i32, cir.ptr <i32>, ["b", init] {alignment = 4 : i64}
%2 = cir.const(2 : i32) : i32
cir.store %2, %0 : i32, cir.ptr <i32>
cir.store %2, %1 : i32, cir.ptr <i32>
%0 = cir.alloca !s32i, cir.ptr <!s32i>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca !s32i, cir.ptr <!s32i>, ["b", init] {alignment = 4 : i64}
%2 = cir.const(#cir.int<2> : !s32i) : !s32i
cir.store %2, %0 : !s32i, cir.ptr <!s32i>
cir.store %2, %1 : !s32i, cir.ptr <!s32i>

%3 = cir.load %0 : cir.ptr <i32>, i32
%4 = cir.unary(inc, %3) : i32, i32
cir.store %4, %0 : i32, cir.ptr <i32>
%3 = cir.load %0 : cir.ptr <!s32i>, !s32i
%4 = cir.unary(inc, %3) : !s32i, !s32i
cir.store %4, %0 : !s32i, cir.ptr <!s32i>

%5 = cir.load %1 : cir.ptr <i32>, i32
%6 = cir.unary(dec, %5) : i32, i32
cir.store %6, %1 : i32, cir.ptr <i32>
%5 = cir.load %1 : cir.ptr <!s32i>, !s32i
%6 = cir.unary(dec, %5) : !s32i, !s32i
cir.store %6, %1 : !s32i, cir.ptr <!s32i>
cir.return
}
}
Expand Down
23 changes: 12 additions & 11 deletions clang/test/CIR/Lowering/ThroughMLIR/unary-plus-minus.cir
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

!s32i = !cir.int<s, 32>
module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca i32, cir.ptr <i32>, ["b", init] {alignment = 4 : i64}
%2 = cir.const(2 : i32) : i32
cir.store %2, %0 : i32, cir.ptr <i32>
cir.store %2, %1 : i32, cir.ptr <i32>
%0 = cir.alloca !s32i, cir.ptr <!s32i>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca !s32i, cir.ptr <!s32i>, ["b", init] {alignment = 4 : i64}
%2 = cir.const(#cir.int<2> : !s32i) : !s32i
cir.store %2, %0 : !s32i, cir.ptr <!s32i>
cir.store %2, %1 : !s32i, cir.ptr <!s32i>

%3 = cir.load %0 : cir.ptr <i32>, i32
%4 = cir.unary(plus, %3) : i32, i32
cir.store %4, %0 : i32, cir.ptr <i32>
%3 = cir.load %0 : cir.ptr <!s32i>, !s32i
%4 = cir.unary(plus, %3) : !s32i, !s32i
cir.store %4, %0 : !s32i, cir.ptr <!s32i>

%5 = cir.load %1 : cir.ptr <i32>, i32
%6 = cir.unary(minus, %5) : i32, i32
cir.store %6, %1 : i32, cir.ptr <i32>
%5 = cir.load %1 : cir.ptr <!s32i>, !s32i
%6 = cir.unary(minus, %5) : !s32i, !s32i
cir.store %6, %1 : !s32i, cir.ptr <!s32i>
cir.return
}
}
Expand Down

0 comments on commit f48cd43

Please sign in to comment.