From 96e5a481852852f45f4c8816b15ff28a35bf84a0 Mon Sep 17 00:00:00 2001 From: Morris Hafner Date: Fri, 25 Apr 2025 17:23:21 +0200 Subject: [PATCH 1/2] Implement codegen for glvalue OpaqueValueExprs --- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 3 ++- clang/test/CIR/CodeGen/ternary.cpp | 25 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 510e5bc75af4..9c997159e774 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -237,7 +237,8 @@ class ScalarExprEmitter : public StmtVisitor { } mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *E) { if (E->isGLValue()) - llvm_unreachable("NYI"); + return emitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E), + E->getExprLoc()); // Otherwise, assume the mapping is the scalar directly. return CGF.getOrCreateOpaqueRValueMapping(E).getScalarVal(); diff --git a/clang/test/CIR/CodeGen/ternary.cpp b/clang/test/CIR/CodeGen/ternary.cpp index 7238459bd520..a313b2a3c629 100644 --- a/clang/test/CIR/CodeGen/ternary.cpp +++ b/clang/test/CIR/CodeGen/ternary.cpp @@ -70,3 +70,28 @@ int foo(int a, int b) { // CHECK: }) : (!cir.bool) -> !s32i // CHECK: [[RES_CAST:%.*]] = cir.cast(int_to_bool, [[RES]] : !s32i), !cir.bool // CHECK: cir.if [[RES_CAST]] + +void maybe_has_side_effects(); + +bool func(bool a, bool b) { + return (maybe_has_side_effects(), a) ?: b; +} + +// CHECK: cir.func @_Z4funcbb(%arg0: !cir.bool +// CHECK: cir.alloca !cir.bool, !cir.ptr, ["a", init] +// CHECK: cir.alloca !cir.bool, !cir.ptr, ["b", init] +// CHECK: cir.alloca !cir.bool, !cir.ptr, ["__retval"] +// CHECK: cir.store %arg0, %0 : !cir.bool, !cir.ptr +// CHECK: cir.store %arg1, %1 : !cir.bool, !cir.ptr +// CHECK: cir.call @_Z22maybe_has_side_effectsv() : () -> () +// CHECK: [[A0:%.*]] = cir.load %0 : !cir.ptr, !cir.bool +// CHECK: [[RES:%.*]] = cir.ternary([[A0]], true { +// CHECK: [[A1:%.*]] = cir.load %0 : !cir.ptr, !cir.bool +// CHECK: cir.yield [[A1]] : !cir.bool +// CHECK: }, false { +// CHECK: [[B0:%.*]] = cir.load %1 : !cir.ptr, !cir.bool +// CHECK: cir.yield [[B0]] : !cir.bool +// CHECK: }) : (!cir.bool) -> !cir.bool +// CHECK: cir.store [[RES]], %2 : !cir.bool, !cir.ptr +// CHECK: [[R:%.*]] = cir.load %2 : !cir.ptr, !cir.bool +// CHECK: cir.return [[R]] : !cir.bool From e05728be97e25d990df8e479920d3e76355bd88a Mon Sep 17 00:00:00 2001 From: Morris Hafner Date: Mon, 28 Apr 2025 20:34:40 +0200 Subject: [PATCH 2/2] Use regex for all SSA registers --- clang/test/CIR/CodeGen/ternary.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/clang/test/CIR/CodeGen/ternary.cpp b/clang/test/CIR/CodeGen/ternary.cpp index a313b2a3c629..3e5fec924a15 100644 --- a/clang/test/CIR/CodeGen/ternary.cpp +++ b/clang/test/CIR/CodeGen/ternary.cpp @@ -77,21 +77,21 @@ bool func(bool a, bool b) { return (maybe_has_side_effects(), a) ?: b; } -// CHECK: cir.func @_Z4funcbb(%arg0: !cir.bool -// CHECK: cir.alloca !cir.bool, !cir.ptr, ["a", init] -// CHECK: cir.alloca !cir.bool, !cir.ptr, ["b", init] -// CHECK: cir.alloca !cir.bool, !cir.ptr, ["__retval"] -// CHECK: cir.store %arg0, %0 : !cir.bool, !cir.ptr -// CHECK: cir.store %arg1, %1 : !cir.bool, !cir.ptr +// CHECK: cir.func @_Z4funcbb([[ARG_A:%.*]]: !cir.bool {{.*}}, [[ARG_B:%.*]]: !cir.bool {{.*}} +// CHECK: [[ALLOC_A:%.*]] = cir.alloca !cir.bool, !cir.ptr, ["a", init] +// CHECK: [[ALLOC_B:%.*]] = cir.alloca !cir.bool, !cir.ptr, ["b", init] +// CHECK: [[ALLOC_RET:%.*]] = cir.alloca !cir.bool, !cir.ptr, ["__retval"] +// CHECK: cir.store [[ARG_A]], [[ALLOC_A]] : !cir.bool, !cir.ptr +// CHECK: cir.store [[ARG_B]], [[ALLOC_B]] : !cir.bool, !cir.ptr // CHECK: cir.call @_Z22maybe_has_side_effectsv() : () -> () -// CHECK: [[A0:%.*]] = cir.load %0 : !cir.ptr, !cir.bool +// CHECK: [[A0:%.*]] = cir.load [[ALLOC_A]] : !cir.ptr, !cir.bool // CHECK: [[RES:%.*]] = cir.ternary([[A0]], true { -// CHECK: [[A1:%.*]] = cir.load %0 : !cir.ptr, !cir.bool +// CHECK: [[A1:%.*]] = cir.load [[ALLOC_A]] : !cir.ptr, !cir.bool // CHECK: cir.yield [[A1]] : !cir.bool // CHECK: }, false { -// CHECK: [[B0:%.*]] = cir.load %1 : !cir.ptr, !cir.bool +// CHECK: [[B0:%.*]] = cir.load [[ALLOC_B]] : !cir.ptr, !cir.bool // CHECK: cir.yield [[B0]] : !cir.bool // CHECK: }) : (!cir.bool) -> !cir.bool -// CHECK: cir.store [[RES]], %2 : !cir.bool, !cir.ptr -// CHECK: [[R:%.*]] = cir.load %2 : !cir.ptr, !cir.bool +// CHECK: cir.store [[RES]], [[ALLOC_RET]] : !cir.bool, !cir.ptr +// CHECK: [[R:%.*]] = cir.load [[ALLOC_RET]] : !cir.ptr, !cir.bool // CHECK: cir.return [[R]] : !cir.bool