-
Notifications
You must be signed in to change notification settings - Fork 182
[CIR] Fix assertion failure in CIRGenRecordLayout for Empty Base Optimization #1999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lanza
wants to merge
5
commits into
gh/lanza/3/base
Choose a base branch
from
gh/lanza/3/head
base: gh/lanza/3/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+101
−2
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lanza
added a commit
that referenced
this pull request
Nov 22, 2025
…mization
This patch fixes a critical assertion failure that occurred when accessing
fields in records that use Empty Base Optimization (EBO), particularly
affecting std::unique_ptr, std::shared_ptr, and other standard library types.
**Problem:**
When processing classes with empty member types combined with inheriting
constructors, CIRGenRecordLayout::getCIRFieldNo() would assert with:
"Assertion `FieldInfo.count(FD) && \"Invalid field for record!\"' failed"
This occurred because EBO transforms empty-type members into base classes
rather than fields, so they don't exist in the FieldInfo map. The bug was
triggered by the combination of three conditions:
1. A member with an empty struct type (e.g., std::default_delete<T>)
2. Inheriting constructors (using Base::Base)
3. Instantiation of the inherited constructor
**Root Cause:**
std::unique_ptr internally uses std::tuple<T*, default_delete<T>>, where
default_delete is an empty struct. When std::tuple applies EBO, the empty
deleter is stored as a base class instead of a member field. During code
generation for constructors, CIR tried to initialize this "field" but it
didn't exist in the FieldInfo map, causing an assertion failure.
**Solution:**
Following the traditional CodeGen pattern from clang/lib/CodeGen, this patch:
1. Adds containsFieldDecl() method to CIRGenRecordLayout (matching
CGRecordLayout::containsFieldDecl) to check if a field exists in the
layout before attempting to access it.
2. Adds guard checks before getCIRFieldNo() calls in:
- CIRGenExpr.cpp::emitLValueForField() - handles field access
- CIRGenExpr.cpp::emitLValueForFieldInitialization() - handles
initialization of reference fields
- CIRGenExprConst.cpp::emitNullConstant() - handles null initialization
- CIRGenExpr.cpp::emitAddrOfFieldStorage() - removes redundant call
3. When a field doesn't exist in the layout (EBO case), returns appropriate
fallback values instead of asserting.
**Impact:**
This fix enables the following previously-broken STL types:
- std::unique_ptr<T>
- std::shared_ptr<T>
- std::function<Sig>
- std::array<T, N>
- std::any
- std::thread
**Testing:**
Added clang/test/CIR/CodeGen/ebo-tuple.cpp which tests the specific
scenario that triggered this bug: empty member types with inheriting
constructors.
Verified with:
- Minimal 24-line reproduction case
- std::unique_ptr<int> default construction
- std::shared_ptr<int> default construction
This implementation strictly follows the traditional CodeGen approach from
clang/lib/CodeGen/CGRecordLayout.h and clang/lib/CodeGen/CGExpr.cpp to
maintain consistency with LLVM CodeGen.
ghstack-source-id: f433d7e
Pull-Request: #1999
This was referenced Nov 22, 2025
This was referenced Nov 22, 2025
lanza
added a commit
that referenced
this pull request
Nov 24, 2025
…mization
This patch fixes a critical assertion failure that occurred when accessing
fields in records that use Empty Base Optimization (EBO), particularly
affecting std::unique_ptr, std::shared_ptr, and other standard library types.
**Problem:**
When processing classes with empty member types combined with inheriting
constructors, CIRGenRecordLayout::getCIRFieldNo() would assert with:
"Assertion `FieldInfo.count(FD) && \"Invalid field for record!\"' failed"
This occurred because EBO transforms empty-type members into base classes
rather than fields, so they don't exist in the FieldInfo map. The bug was
triggered by the combination of three conditions:
1. A member with an empty struct type (e.g., std::default_delete<T>)
2. Inheriting constructors (using Base::Base)
3. Instantiation of the inherited constructor
**Root Cause:**
std::unique_ptr internally uses std::tuple<T*, default_delete<T>>, where
default_delete is an empty struct. When std::tuple applies EBO, the empty
deleter is stored as a base class instead of a member field. During code
generation for constructors, CIR tried to initialize this "field" but it
didn't exist in the FieldInfo map, causing an assertion failure.
**Solution:**
Following the traditional CodeGen pattern from clang/lib/CodeGen, this patch:
1. Adds containsFieldDecl() method to CIRGenRecordLayout (matching
CGRecordLayout::containsFieldDecl) to check if a field exists in the
layout before attempting to access it.
2. Adds guard checks before getCIRFieldNo() calls in:
- CIRGenExpr.cpp::emitLValueForField() - handles field access
- CIRGenExpr.cpp::emitLValueForFieldInitialization() - handles
initialization of reference fields
- CIRGenExprConst.cpp::emitNullConstant() - handles null initialization
- CIRGenExpr.cpp::emitAddrOfFieldStorage() - removes redundant call
3. When a field doesn't exist in the layout (EBO case), returns appropriate
fallback values instead of asserting.
**Impact:**
This fix enables the following previously-broken STL types:
- std::unique_ptr<T>
- std::shared_ptr<T>
- std::function<Sig>
- std::array<T, N>
- std::any
- std::thread
**Testing:**
Added clang/test/CIR/CodeGen/ebo-tuple.cpp which tests the specific
scenario that triggered this bug: empty member types with inheriting
constructors.
Verified with:
- Minimal 24-line reproduction case
- std::unique_ptr<int> default construction
- std::shared_ptr<int> default construction
This implementation strictly follows the traditional CodeGen approach from
clang/lib/CodeGen/CGRecordLayout.h and clang/lib/CodeGen/CGExpr.cpp to
maintain consistency with LLVM CodeGen.
ghstack-source-id: 5d2b410
Pull-Request: #1999
This was referenced Nov 24, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
This patch fixes a critical assertion failure that occurred when accessing
fields in records that use Empty Base Optimization (EBO), particularly
affecting std::unique_ptr, std::shared_ptr, and other standard library types.
Problem:
When processing classes with empty member types combined with inheriting
constructors, CIRGenRecordLayout::getCIRFieldNo() would assert with:
"Assertion `FieldInfo.count(FD) && "Invalid field for record!"' failed"
This occurred because EBO transforms empty-type members into base classes
rather than fields, so they don't exist in the FieldInfo map. The bug was
triggered by the combination of three conditions:
Root Cause:
std::unique_ptr internally uses std::tuple<T*, default_delete>, where
default_delete is an empty struct. When std::tuple applies EBO, the empty
deleter is stored as a base class instead of a member field. During code
generation for constructors, CIR tried to initialize this "field" but it
didn't exist in the FieldInfo map, causing an assertion failure.
Solution:
Following the traditional CodeGen pattern from clang/lib/CodeGen, this patch:
Adds containsFieldDecl() method to CIRGenRecordLayout (matching
CGRecordLayout::containsFieldDecl) to check if a field exists in the
layout before attempting to access it.
Adds guard checks before getCIRFieldNo() calls in:
initialization of reference fields
When a field doesn't exist in the layout (EBO case), returns appropriate
fallback values instead of asserting.
Impact:
This fix enables the following previously-broken STL types:
Testing:
Added clang/test/CIR/CodeGen/ebo-tuple.cpp which tests the specific
scenario that triggered this bug: empty member types with inheriting
constructors.
Verified with:
This implementation strictly follows the traditional CodeGen approach from
clang/lib/CodeGen/CGRecordLayout.h and clang/lib/CodeGen/CGExpr.cpp to
maintain consistency with LLVM CodeGen.