Author: Aman Maurya Date: 2026-10-02T06:24:45+05:30 New Revision: 52638665f52fa7254076d1acc9e1191c6fa1c0b3
URL: https://github.com/llvm/llvm-project/commit/52638665f52fa7254076d1acc9e1191c6fa1c0b3 DIFF: https://github.com/llvm/llvm-project/commit/52638665f52fa7254076d1acc9e1191c6fa1c0b3.diff LOG: [CIR] Support CXXThisExpr in emitLValue (#227316) Support `CXXThisExpr` in `CIRGenFunction::emitLValue` by wrapping `loadCXXThisAddress()` into an LValue via `makeAddrLValue()`, matching classic Clang codegen (`CGExpr.cpp:1865`). This enables LValue contexts for `this`, such as member access expressions via `this.field` in languages like HLSL where `this` is reference-like. Fixes #227193 Added: clang/test/CIR/CodeGenHLSL/cxx-this-lvalue.hlsl Modified: clang/lib/CIR/CodeGen/CIRGenFunction.cpp Removed: ################################################################################ diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index 823ccbe1d2e82..ca4cf4be63de3 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -1301,8 +1301,7 @@ LValue CIRGenFunction::emitLValue(const Expr *e) { "emitLValue: MatrixElementExpr"); return LValue(); case Expr::CXXThisExprClass: - getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: CXXThisExpr"); - return LValue(); + return makeAddrLValue(loadCXXThisAddress(), e->getType()); case Expr::MemberExprClass: return emitMemberExpr(cast<MemberExpr>(e)); case Expr::CompoundLiteralExprClass: diff --git a/clang/test/CIR/CodeGenHLSL/cxx-this-lvalue.hlsl b/clang/test/CIR/CodeGenHLSL/cxx-this-lvalue.hlsl new file mode 100644 index 0000000000000..c841e64de95f6 --- /dev/null +++ b/clang/test/CIR/CodeGenHLSL/cxx-this-lvalue.hlsl @@ -0,0 +1,61 @@ +// RUN: %clang_cc1 -x hlsl -triple spirv-unknown-vulkan-library -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -x hlsl -triple spirv-unknown-vulkan-library -fclangir -emit-llvm %s -o %t.cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.cir.ll %s +// RUN: %clang_cc1 -x hlsl -triple spirv-unknown-vulkan-library -emit-llvm -disable-llvm-passes %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +struct S { + int a; + + int getA() { + return this.a; + } + + void setA(int v) { + this.a = v; + } +}; + +export int testGet() { + S s; + return s.getA(); +} + +// CIR-LABEL: cir.func {{.*}} @_ZN1S4getAEv +// CIR: %[[THIS_ADDR:.*]] = cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>> +// CIR: %[[THIS:.*]] = cir.load %[[THIS_ADDR]] : !cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S> +// CIR: %[[MEMBER:.*]] = cir.get_member %[[THIS]][0] {name = "a"} : !cir.ptr<!rec_S> -> !cir.ptr<!s32i> +// CIR: %[[VAL:.*]] = cir.load {{.*}} %[[MEMBER]] : !cir.ptr<!s32i>, !s32i +// CIR: cir.store %[[VAL]], %[[RETVAL:.*]] : !s32i, !cir.ptr<!s32i> +// CIR: cir.return {{.*}} : !s32i + +// LLVM-LABEL: define {{.*}} @_ZN1S4getAEv( +// LLVM-SAME: ptr {{.*}} %[[ARG0:.*]]) +// LLVM: %[[THIS_ADDR:.*]] = alloca ptr +// LLVM: store ptr %[[ARG0]], ptr %[[THIS_ADDR]] +// LLVM: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]] +// LLVM: %[[GEP:.*]] = getelementptr inbounds nuw %struct.S, ptr %[[THIS]], i32 0, i32 0 +// LLVM: %[[VAL:.*]] = load i32, ptr %[[GEP]] +// LLVM: ret i32 %{{.*}} + +export void testSet(int v) { + S s; + s.setA(v); +} + +// CIR-LABEL: cir.func {{.*}} @_ZN1S4setAEi +// CIR: %[[THIS_ADDR:.*]] = cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>> +// CIR: %[[THIS:.*]] = cir.load %[[THIS_ADDR]] : !cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S> +// CIR: %[[MEMBER:.*]] = cir.get_member %[[THIS]][0] {name = "a"} : !cir.ptr<!rec_S> -> !cir.ptr<!s32i> +// CIR: cir.store align(1) {{.*}}, %[[MEMBER]] : !s32i, !cir.ptr<!s32i> +// CIR: cir.return + +// LLVM-LABEL: define {{.*}} @_ZN1S4setAEi( +// LLVM-SAME: ptr {{.*}} %[[ARG0:.*]], i32 {{.*}} %[[ARG1:.*]]) +// LLVM: %[[THIS_ADDR:.*]] = alloca ptr +// LLVM: store ptr %[[ARG0]], ptr %[[THIS_ADDR]] +// LLVM: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]] +// LLVM: %[[GEP:.*]] = getelementptr inbounds nuw %struct.S, ptr %[[THIS]], i32 0, i32 0 +// LLVM: store i32 {{.*}}, ptr %[[GEP]] +// LLVM: ret void _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
