Skip to content

Commit 95e4dc6

Browse files
authored
[CIR] Add support for the RequiresExpr (#171818)
Add support for the RequiresExpr
1 parent fa79e0a commit 95e4dc6

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
852852
return builder.getBool(e->isSatisfied(), cgf.getLoc(e->getExprLoc()));
853853
}
854854
mlir::Value VisitRequiresExpr(const RequiresExpr *e) {
855-
cgf.cgm.errorNYI(e->getSourceRange(), "ScalarExprEmitter: requires");
856-
return {};
855+
return builder.getBool(e->isSatisfied(), cgf.getLoc(e->getExprLoc()));
857856
}
858857
mlir::Value VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *e) {
859858
cgf.cgm.errorNYI(e->getSourceRange(),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7+
8+
template <typename T> void summable(T a) {
9+
if (requires { a + a; }) {
10+
T b = a + a;
11+
}
12+
}
13+
14+
// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
15+
// CIR: cir.store %[[ARG_A:.*]], %[[A_ADDR]] : !s32i, !cir.ptr<!s32i>
16+
// CIR: cir.scope {
17+
// CIR: %[[CONST_TRUE:.*]] = cir.const #true
18+
// CIR: cir.if %[[CONST_TRUE]] {
19+
// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
20+
// CIR: %[[TMP_A_1:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
21+
// CIR: %[[TMP_A_2:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
22+
// CIR: %[[RESULT:.*]] = cir.binop(add, %[[TMP_A_1]], %[[TMP_A_2]]) nsw : !s32i
23+
// CIR: cir.store {{.*}} %[[RESULT]], %[[B_ADDR]] : !s32i, !cir.ptr<!s32i>
24+
// CIR: }
25+
// CIR: }
26+
27+
// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
28+
// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4
29+
// LLVM: store i32 %[[ARG_A:.*]], ptr %[[A_ADDR]], align 4
30+
// LLVM: br label %[[IF_COND:.*]]
31+
// LLVM: [[IF_COND]]:
32+
// LLVM: br i1 true, label %[[IF_THEN:.*]], label %[[IF_END:.*]]
33+
// LLVM: [[IF_THEN]]:
34+
// LLVM: %[[TMP_A_1:.*]] = load i32, ptr %[[A_ADDR]], align 4
35+
// LLVM: %[[TMP_A_2:.*]] = load i32, ptr %[[A_ADDR]], align 4
36+
// LLVM: %[[RESULT:.*]] = add nsw i32 %[[TMP_A_1]], %[[TMP_A_2]]
37+
// LLVM: store i32 %[[RESULT]], ptr %[[B_ADDR]], align 4
38+
// LLVM: br label %[[IF_END]]
39+
// LLVM: [[IF_END]]:
40+
// LLVM: br label %[[RET:.*]]
41+
42+
// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4
43+
// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
44+
// OGCG: store i32 %[[ARG_A:.*]], ptr %[[A_ADDR]], align 4
45+
// OGCG: %[[TMP_A_1:.*]] = load i32, ptr %[[A_ADDR]], align 4
46+
// OGCG: %[[TMP_A_2:.*]] = load i32, ptr %[[A_ADDR]], align 4
47+
// OGCG: %[[RESULT:.*]] = add nsw i32 %[[TMP_A_1]], %[[TMP_A_2]]
48+
// OGCG: store i32 %[[RESULT]], ptr %[[B_ADDR]], align 4
49+
50+
void call_function_with_requires_expr() { summable(1); }
51+

0 commit comments

Comments
 (0)