llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

... if we don't have a block scope available. This can happen in `EvalEmitter` 
scenarios and can cause local variable blocks to be prematurely converted to 
dead blocks. Attach `ScopeKind::Block` variable to the root scope instead.

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.h (+7) 
- (modified) clang/test/AST/ByteCode/codegen-constexpr-unknown.cpp (+18) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.h 
b/clang/lib/AST/ByteCode/Compiler.h
index 1bd15c3d79563..8f1f9dad4469e 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -490,10 +490,17 @@ template <class Emitter> class VariableScope {
   void addForScopeKind(const Scope::Local &Local, ScopeKind Kind) {
     VariableScope *P = this;
     while (P) {
+      // We found the right scope kind.
       if (P->Kind == Kind) {
         P->addLocal(Local);
         return;
       }
+      // If we reached the root scope and we're looking for a Block scope,
+      // attach it to the root instead of the current scope.
+      if (!P->Parent && Kind == ScopeKind::Block) {
+        P->addLocal(Local);
+        return;
+      }
       P = P->Parent;
       if (!P)
         break;
diff --git a/clang/test/AST/ByteCode/codegen-constexpr-unknown.cpp 
b/clang/test/AST/ByteCode/codegen-constexpr-unknown.cpp
index f62117d5f7bec..e3f145a3dd0d0 100644
--- a/clang/test/AST/ByteCode/codegen-constexpr-unknown.cpp
+++ b/clang/test/AST/ByteCode/codegen-constexpr-unknown.cpp
@@ -70,3 +70,21 @@ X test24() {
 // CHECK: define dso_local void @_Z6test24v
 // CHECK-NOT: lpad
 // CHECK-NOT: eh.resume
+
+/// CodeGenFunction::ConstantFoldsToSimpleInteger() for the if condition
+/// needs to succeed and return true.
+extern void abort2();
+constexpr const int* to_address(const int *p) {
+  return p;
+}
+void rightscope() {
+  const int p = 0;
+  if (to_address(&p) == &p)
+    return;
+  abort2();
+}
+// CHECK:      define dso_local void @_Z10rightscopev()
+// CHECK-NEXT: entry:
+// CHECK-NEXT: %p = alloca i32, align 4
+// CHECK-NEXT: store i32 0, ptr %p, align 4
+// CHECK-NEXT: ret void

``````````

</details>


https://github.com/llvm/llvm-project/pull/183279
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to