Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
gitoleg committed May 7, 2024
1 parent 739683a commit 4f4f32a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 32 deletions.
3 changes: 2 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3677,7 +3677,8 @@ def GotoOp : CIR_Op<"goto", [Terminator]> {
// LabelOp
//===----------------------------------------------------------------------===//

def LabelOp : CIR_Op<"label"> {
// The LabelOp has AlwaysSpeculatable trait in order to not to be swept by canonicalizer
def LabelOp : CIR_Op<"label", [AlwaysSpeculatable]> {
let description = [{ An identifier which may be referred by cir.goto operation }];
let arguments = (ins StrAttr:$label);
let assemblyFormat = [{ $label attr-dict }];
Expand Down
79 changes: 48 additions & 31 deletions clang/test/CIR/Lowering/goto.cir
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
// RUN: cir-opt %s -canonicalize -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -canonicalize -o - | cir-translate -cir-to-llvmir | FileCheck %s -check-prefix=LLVM
// RUN: cir-opt %s --pass-pipeline='builtin.module(cir-to-llvm,canonicalize{region-simplify=false})' -o - | FileCheck %s -check-prefix=MLIR

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

module {
cir.func @foo() {
%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<!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<!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

cir.func @gotoFromIf(%arg0: !s32i) -> !s32i {
%0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init] {alignment = 4 : i64}
%1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64}
cir.store %arg0, %0 : !s32i, !cir.ptr<!s32i>
cir.scope {
%6 = cir.load %0 : !cir.ptr<!s32i>, !s32i
%7 = cir.const(#cir.int<5> : !s32i) : !s32i
%8 = cir.cmp(gt, %6, %7) : !s32i, !s32i
%9 = cir.cast(int_to_bool, %8 : !s32i), !cir.bool
cir.if %9 {
cir.goto "err"
}
}
%2 = cir.const(#cir.int<0> : !s32i) : !s32i
cir.store %2, %1 : !s32i, !cir.ptr<!s32i>
cir.br ^bb1
^bb1:
%3 = cir.load %1 : !cir.ptr<!s32i>, !s32i
cir.return %3 : !s32i
^bb2:
cir.label "err"
%4 = cir.const(#cir.int<1> : !s32i) : !s32i
%5 = cir.unary(minus, %4) : !s32i, !s32i
cir.store %5, %1 : !s32i, !cir.ptr<!s32i>
cir.br ^bb1
}
}

// MLIR: module {
// MLIR-NEXT: llvm.func @foo
// MLIR: llvm.br ^bb1
// MLIR: ^bb1:
// MLIR: return

// LLVM: br label %[[Value:[0-9]+]]
// LLVM-EMPTY:
// LLVM-NEXT: [[Value]]: ; preds =
// LLVM: ret void
// MLIR: llvm.func @gotoFromIf
// MLIR: %[[#One:]] = llvm.mlir.constant(1 : i32) : i32
// MLIR: %[[#Zero:]] = llvm.mlir.constant(0 : i32) : i32
// MLIR: llvm.cond_br {{.*}}, ^bb[[#COND_YES:]], ^bb[[#COND_NO:]]
// MLIR: ^bb[[#COND_YES]]:
// MLIR: llvm.br ^bb[[#GOTO_BLK:]]
// MLIR: ^bb[[#COND_NO]]:
// MLIR: llvm.br ^bb[[#BLK:]]
// MLIR: ^bb[[#BLK]]:
// MLIR: llvm.store %[[#Zero]], %[[#Ret_val_addr:]] : i32, !llvm.ptr
// MLIR: llvm.br ^bb[[#RETURN:]]
// MLIR: ^bb[[#RETURN]]:
// MLIR: %[[#Ret_val:]] = llvm.load %[[#Ret_val_addr]] : !llvm.ptr -> i32
// MLIR: llvm.return %[[#Ret_val]] : i32
// MLIR: ^bb[[#GOTO_BLK]]:
// MLIR: %[[#Neg_one:]] = llvm.sub %[[#Zero]], %[[#One]] : i32
// MLIR: llvm.store %[[#Neg_one]], %[[#Ret_val_addr]] : i32, !llvm.ptr
// MLIR: llvm.br ^bb[[#RETURN]]
// MLIR: }
}
38 changes: 38 additions & 0 deletions clang/test/CIR/Lowering/region-simplify.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: cir-opt %s -canonicalize -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-opt %s -canonicalize -o - | cir-translate -cir-to-llvmir | FileCheck %s -check-prefix=LLVM

!u32i = !cir.int<u, 32>

module {
cir.func @foo() {
%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<!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<!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
}

// MLIR: module {
// MLIR-NEXT: llvm.func @foo
// MLIR: llvm.br ^bb1
// MLIR: ^bb1:
// MLIR: return

// LLVM: br label %[[Value:[0-9]+]]
// LLVM-EMPTY:
// LLVM-NEXT: [[Value]]: ; preds =
// LLVM: ret void


}

0 comments on commit 4f4f32a

Please sign in to comment.