[clang] [clang][CodeGen] Run Bypasses.Init() in StartFunction (PR #244723)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 20 13:53:05 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
Author: Nick Desaulniers (nickdesaulniers)
<details>
<summary>Changes</summary>
Move ShouldEmitLifetimeMarkers coroutine check and Bypasses.Init() from
CodeGenFunction::GenerateCode() into CodeGenFunction::StartFunction() so
non-FunctionDecl bodies (such as CapturedDecl, blocks, and ObjC methods)
also run bypass detection.
Add a regression test for CapturedDecl in
clang/test/CodeGen/trivial-auto-var-init-bypass.c.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/244723.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+13-14)
- (modified) clang/test/CodeGen/trivial-auto-var-init-bypass.c (+50)
``````````diff
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index e9ec756e0233d..5d4de45a8f2fc 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -763,6 +763,19 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
if (FD && FD->usesSEHTry())
CurSEHParent = GD;
+ if (const Stmt *Body = D ? D->getBody() : nullptr) {
+ // Coroutines always emit lifetime markers.
+ if (isa<CoroutineBodyStmt>(Body))
+ ShouldEmitLifetimeMarkers = true;
+
+ // Detect jumps that invalidate lifetime markers or bypass auto-var-init.
+ bool NeedsBypassDetection =
+ ShouldEmitLifetimeMarkers ||
+ (CGM.getLangOpts().getTrivialAutoVarInit() !=
+ LangOptions::TrivialAutoVarInitKind::Uninitialized);
+ if (NeedsBypassDetection)
+ Bypasses.Init(CGM, Body);
+ }
CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
FnRetTy = RetTy;
CurFn = Fn;
@@ -1577,20 +1590,6 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
Stmt *Body = FD->getBody();
- if (Body) {
- // Coroutines always emit lifetime markers.
- if (isa<CoroutineBodyStmt>(Body))
- ShouldEmitLifetimeMarkers = true;
-
- // Detect jumps that invalidate lifetime markers or bypass auto-var-init.
- bool NeedsBypassDetection =
- ShouldEmitLifetimeMarkers ||
- (CGM.getLangOpts().getTrivialAutoVarInit() !=
- LangOptions::TrivialAutoVarInitKind::Uninitialized);
- if (NeedsBypassDetection)
- Bypasses.Init(CGM, Body);
- }
-
// Emit the standard function prologue.
StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
diff --git a/clang/test/CodeGen/trivial-auto-var-init-bypass.c b/clang/test/CodeGen/trivial-auto-var-init-bypass.c
index e5cd5662091c0..7014f8d5e6093 100644
--- a/clang/test/CodeGen/trivial-auto-var-init-bypass.c
+++ b/clang/test/CodeGen/trivial-auto-var-init-bypass.c
@@ -812,6 +812,56 @@ void asm_goto_with_output_bypass(void) {
use(&x);
use(&out);
}
+
+// Bypass detection also runs for non-FunctionDecl bodies such as CapturedDecl.
+// ZERO-LABEL: define dso_local void @captured_stmt_bypass(
+// ZERO-SAME: i32 noundef [[C:%.*]]) #[[ATTR0]] {
+// ZERO-NEXT: [[ENTRY:.*:]]
+// ZERO-NEXT: [[C_ADDR:%.*]] = alloca i32, align 4
+// ZERO-NEXT: [[AGG_CAPTURED:%.*]] = alloca [[STRUCT_ANON:%.*]], align 8
+// ZERO-NEXT: store i32 [[C]], ptr [[C_ADDR]], align 4
+// ZERO-NEXT: [[TMP0:%.*]] = getelementptr inbounds nuw [[STRUCT_ANON]], ptr [[AGG_CAPTURED]], i32 0, i32 0
+// ZERO-NEXT: store ptr [[C_ADDR]], ptr [[TMP0]], align 8
+// ZERO-NEXT: call void @__captured_stmt(ptr [[AGG_CAPTURED]])
+// ZERO-NEXT: ret void
+//
+// PATTERN-LABEL: define dso_local void @captured_stmt_bypass(
+// PATTERN-SAME: i32 noundef [[C:%.*]]) #[[ATTR0]] {
+// PATTERN-NEXT: [[ENTRY:.*:]]
+// PATTERN-NEXT: [[C_ADDR:%.*]] = alloca i32, align 4
+// PATTERN-NEXT: [[AGG_CAPTURED:%.*]] = alloca [[STRUCT_ANON:%.*]], align 8
+// PATTERN-NEXT: store i32 [[C]], ptr [[C_ADDR]], align 4
+// PATTERN-NEXT: [[TMP0:%.*]] = getelementptr inbounds nuw [[STRUCT_ANON]], ptr [[AGG_CAPTURED]], i32 0, i32 0
+// PATTERN-NEXT: store ptr [[C_ADDR]], ptr [[TMP0]], align 8
+// PATTERN-NEXT: call void @__captured_stmt(ptr [[AGG_CAPTURED]])
+// PATTERN-NEXT: ret void
+//
+void captured_stmt_bypass(int c) {
+#pragma clang __debug captured
+ {
+ if (c)
+ goto SKIP;
+ int x;
+ SKIP:
+ use(&x);
+ }
+}
+// ZERO-LABEL: define internal void @__captured_stmt(
+// ZERO: if.then:
+// ZERO-NEXT: store i32 0, ptr %x, align 4, !annotation [[META1]]
+// ZERO-NEXT: br label %SKIP
+// ZERO: if.end:
+// ZERO-NEXT: store i32 0, ptr %x, align 4, !annotation [[META1]]
+// ZERO-NEXT: br label %SKIP
+//
+// PATTERN-LABEL: define internal void @__captured_stmt(
+// PATTERN: if.then:
+// PATTERN-NEXT: store i32 -1431655766, ptr %x, align 4, !annotation [[META1]]
+// PATTERN-NEXT: br label %SKIP
+// PATTERN: if.end:
+// PATTERN-NEXT: store i32 -1431655766, ptr %x, align 4, !annotation [[META1]]
+// PATTERN-NEXT: br label %SKIP
+//
//.
// ZERO: [[META1]] = !{!"auto-init"}
// ZERO: [[LOOP2]] = distinct !{[[LOOP2]], [[META3:![0-9]+]]}
``````````
</details>
https://github.com/llvm/llvm-project/pull/244723
More information about the cfe-commits
mailing list