[clang] [clang][CUDA] Perform lvalue-to-rvalue conversion on `__cluster_dims__` attribute (PR #227998)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 1 02:00:26 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

A `ConstantExpr` with an lvalue  subexpression should always have an lvalue APValue result. 

---
Full diff: https://github.com/llvm/llvm-project/pull/227998.diff


2 Files Affected:

- (modified) clang/include/clang/AST/Expr.h (+11-1) 
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+13-3) 


``````````diff
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index a0324240ce34d..ed33fc93f2383 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1095,8 +1095,12 @@ class FullExpr : public Expr {
 /// Describes the kind of result that can be tail-allocated.
 enum class ConstantResultStorageKind { None, Int64, APValue };
 
-/// ConstantExpr - An expression that occurs in a constant context and
+/// An expression that occurs in a constant context and
 /// optionally the result of evaluating the expression.
+///
+/// \invariant The subexpression of a ConstantExpr and its APValue must have the
+/// same value category. In practice, this means that an lvalue subexpression
+/// must also have an APValue of type LValue.
 class ConstantExpr final
     : public FullExpr,
       private llvm::TrailingObjects<ConstantExpr, APValue, uint64_t> {
@@ -1161,6 +1165,12 @@ class ConstantExpr final
   }
 
   void SetResult(APValue Value, const ASTContext &Context) {
+    if (getSubExpr()->isGLValue())
+      assert(Value.isLValue() &&
+             "SubExpr and APValue value category must match");
+    else
+      assert(getSubExpr()->isPRValue());
+
     MoveIntoResult(Value, Context);
   }
   void MoveIntoResult(APValue &Value, const ASTContext &Context);
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index eb4a8c2ab9ae0..3fce8589739cd 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6261,9 +6261,19 @@ static void handleClusterDimsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
       !AL.checkAtMostNumArgs(S, /*Num=*/3))
     return;
 
-  S.addClusterDimsAttr(D, AL, AL.getArgAsExpr(0),
-                       AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
-                       AL.getNumArgs() > 2 ? AL.getArgAsExpr(2) : nullptr);
+  // Convert all arguments to rvalues.
+  ExprResult Converted1 = S.DefaultLvalueConversion(AL.getArgAsExpr(0));
+  assert(!Converted1.isInvalid());
+  ExprResult Converted2 = AL.getNumArgs() > 1
+                              ? S.DefaultLvalueConversion(AL.getArgAsExpr(1))
+                              : ExprResult();
+  ExprResult Converted3 = AL.getNumArgs() > 2
+                              ? S.DefaultLvalueConversion(AL.getArgAsExpr(2))
+                              : ExprResult();
+
+  S.addClusterDimsAttr(D, AL, Converted1.get(),
+                       Converted2.isInvalid() ? nullptr : Converted2.get(),
+                       Converted3.isInvalid() ? nullptr : Converted3.get());
 }
 
 static void handleNoClusterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/227998


More information about the cfe-commits mailing list