[clang] [HLSL] Handle `this` in dynamic resource indexing validation (PR #239876)
Helena Kotas via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 17:57:38 PST 2026
https://github.com/hekota created https://github.com/llvm/llvm-project/pull/239876
Fixes #239858
>From 216adec5580a31f4f6d085f473e5c7a51e851f31 Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Fri, 6 Nov 2026 17:56:29 -0800
Subject: [PATCH] [HLSL] Handle `this` in dynamic resource indexing validation
Fixes #239858
---
clang/lib/Sema/SemaHLSL.cpp | 2 ++
.../resources-in-structs-this-access.hlsl | 30 +++++++++++++++++++
2 files changed, 32 insertions(+)
create mode 100644 clang/test/CodeGenHLSL/resources/resources-in-structs-this-access.hlsl
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index eb6fce49d8708..2a57c82e59da8 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -3642,6 +3642,8 @@ bool SemaHLSL::ActOnResourceMemberAccessExpr(MemberExpr *ME) {
E = SubME->getBase();
} else if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
E = ICE->getSubExpr();
+ } else if (isa<CXXThisExpr>(E)) {
+ return true;
} else {
llvm_unreachable("unexpected expr type in resource member access");
}
diff --git a/clang/test/CodeGenHLSL/resources/resources-in-structs-this-access.hlsl b/clang/test/CodeGenHLSL/resources/resources-in-structs-this-access.hlsl
new file mode 100644
index 0000000000000..9dedb33fb7542
--- /dev/null
+++ b/clang/test/CodeGenHLSL/resources/resources-in-structs-this-access.hlsl
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -finclude-default-header -emit-llvm -disable-llvm-passes -o - %s | llvm-cxxfilt | FileCheck %s
+
+// Regression test for issue #239858
+
+// CHECK: %"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", int, 1, 0, 0) }
+
+struct A {
+ RWBuffer<int> Buf;
+
+ int get(uint Index) const {
+ return this.Buf[Index] + this.Buf.Load(Index);;
+ }
+};
+
+// Global variables for resources in `a` and `array`
+//
+// CHECK-DAG: @a.Buf = internal global %"class.hlsl::RWBuffer" poison, align 4
+// CHECK-DAG: @[[aBufStr:.*]] = private unnamed_addr constant [7 x i8] c"a.Buf\00", align 1
+// CHECK-DAG: @array.0.Buf = internal global %"class.hlsl::RWBuffer" poison, align 4
+// CHECK-DAG: @[[array0BufStr:.*]] = private unnamed_addr constant [12 x i8] c"array.0.Buf\00", align 1
+// CHECK-DAG: @array.1.Buf = internal global %"class.hlsl::RWBuffer" poison, align 4
+// CHECK-DAG: @[[array1BufStr:.*]] = private unnamed_addr constant [12 x i8] c"array.1.Buf\00", align 1
+
+A a : register(u5);
+A array[2];
+
+[numthreads(8, 1, 1)]
+void main(uint3 ID : SV_GroupThreadID) {
+ a.Buf[ID.x] = a.get(ID.x) + array[1].get(ID.x);
+}
More information about the cfe-commits
mailing list