-
Notifications
You must be signed in to change notification settings - Fork 182
[CIR] Implement constant expression member pointer casts #2017
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
1
commit into
gh/lanza/17/base
Choose a base branch
from
gh/lanza/17/head
base: gh/lanza/17/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.
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 24, 2025
This patch implements support for member pointer casts in constant
expressions, specifically handling CK_DerivedToBaseMemberPointer,
CK_BaseToDerivedMemberPointer, and CK_ReinterpretMemberPointer cast kinds.
The implementation closely follows CodeGen's approach in CGExprConstant.cpp
and ItaniumCXXABI.cpp, adapted for CIR's attribute-based constant model.
Implementation details:
1. Added CIRGenModule::GetNonVirtualBaseClassOffset() - Computes the offset
between derived and base classes as a constant cir::IntAttr, mirroring
CodeGen's GetNonVirtualBaseClassOffset() which returns llvm::Constant.
2. Added CIRGenCXXABI::getMemberPointerAdjustment() - Calculates the
adjustment needed for member pointer conversions by determining the
derived type based on cast direction and delegating to
GetNonVirtualBaseClassOffset().
3. Added CIRGenCXXABI::EmitMemberPointerConversion() - Base virtual method
with NYI default, to be overridden by specific ABI implementations.
4. Implemented CIRGenItaniumCXXABI::EmitMemberPointerConversion() for the
Itanium C++ ABI:
- CK_ReinterpretMemberPointer: Returns source unchanged (no adjustment)
- CK_DerivedToBaseMemberPointer/CK_BaseToDerivedMemberPointer:
* For member data pointers: Adjusts the cir::DataMemberAttr index
by the offset, matching LLVM's nsw add/sub on constant integers
* Member function pointers: Marked NYI (requires struct {ptr, adj})
5. Updated ConstExprEmitter::VisitCastExpr() to call the new infrastructure
instead of llvm_unreachable().
Differences from CodeGen:
- Uses CIR's attribute system (cir::IntAttr, cir::DataMemberAttr) instead
of LLVM constants
- Member data pointer adjustment directly modifies the DataMemberAttr
index rather than operating on raw integer constants
Test Plan:
- Added clang/test/CIR/CodeGen/member-pointer-cast.cpp to verify CIR output
- Updated clang/test/CIR/crashes/constexpr-cast.cpp to reflect progress
(original NYI at line 1006 is fixed; now fails at member function pointer
emission from APValue, which is a separate feature)
The crash test case from TODO.md item #9 now progresses past the constant
expression cast NYI to a different unimplemented feature (member function
pointer emission), demonstrating that the core cast infrastructure works.
Fixes original NYI at CIRGenExprConst.cpp:1006
ghstack-source-id: cf00c7d
Pull-Request: #2017
This was referenced Nov 24, 2025
lanza
added a commit
that referenced
this pull request
Nov 24, 2025
This patch implements support for member pointer casts in constant
expressions, specifically handling CK_DerivedToBaseMemberPointer,
CK_BaseToDerivedMemberPointer, and CK_ReinterpretMemberPointer cast kinds.
The implementation closely follows CodeGen's approach in CGExprConstant.cpp
and ItaniumCXXABI.cpp, adapted for CIR's attribute-based constant model.
Implementation details:
1. Added CIRGenModule::GetNonVirtualBaseClassOffset() - Computes the offset
between derived and base classes as a constant cir::IntAttr, mirroring
CodeGen's GetNonVirtualBaseClassOffset() which returns llvm::Constant.
2. Added CIRGenCXXABI::getMemberPointerAdjustment() - Calculates the
adjustment needed for member pointer conversions by determining the
derived type based on cast direction and delegating to
GetNonVirtualBaseClassOffset().
3. Added CIRGenCXXABI::EmitMemberPointerConversion() - Base virtual method
with NYI default, to be overridden by specific ABI implementations.
4. Implemented CIRGenItaniumCXXABI::EmitMemberPointerConversion() for the
Itanium C++ ABI:
- CK_ReinterpretMemberPointer: Returns source unchanged (no adjustment)
- CK_DerivedToBaseMemberPointer/CK_BaseToDerivedMemberPointer:
* For member data pointers: Adjusts the cir::DataMemberAttr index
by the offset, matching LLVM's nsw add/sub on constant integers
* Member function pointers: Marked NYI (requires struct {ptr, adj})
5. Updated ConstExprEmitter::VisitCastExpr() to call the new infrastructure
instead of llvm_unreachable().
Differences from CodeGen:
- Uses CIR's attribute system (cir::IntAttr, cir::DataMemberAttr) instead
of LLVM constants
- Member data pointer adjustment directly modifies the DataMemberAttr
index rather than operating on raw integer constants
Test Plan:
- Added clang/test/CIR/CodeGen/member-pointer-cast.cpp to verify CIR output
- Updated clang/test/CIR/crashes/constexpr-cast.cpp to reflect progress
(original NYI at line 1006 is fixed; now fails at member function pointer
emission from APValue, which is a separate feature)
The crash test case from TODO.md item #9 now progresses past the constant
expression cast NYI to a different unimplemented feature (member function
pointer emission), demonstrating that the core cast infrastructure works.
Fixes original NYI at CIRGenExprConst.cpp:1006
ghstack-source-id: cf00c7d
Pull-Request: #2017
lanza
added a commit
that referenced
this pull request
Nov 24, 2025
This patch implements support for member pointer casts in constant
expressions, specifically handling CK_DerivedToBaseMemberPointer,
CK_BaseToDerivedMemberPointer, and CK_ReinterpretMemberPointer cast kinds.
The implementation closely follows CodeGen's approach in CGExprConstant.cpp
and ItaniumCXXABI.cpp, adapted for CIR's attribute-based constant model.
Implementation details:
1. Added CIRGenModule::GetNonVirtualBaseClassOffset() - Computes the offset
between derived and base classes as a constant cir::IntAttr, mirroring
CodeGen's GetNonVirtualBaseClassOffset() which returns llvm::Constant.
2. Added CIRGenCXXABI::getMemberPointerAdjustment() - Calculates the
adjustment needed for member pointer conversions by determining the
derived type based on cast direction and delegating to
GetNonVirtualBaseClassOffset().
3. Added CIRGenCXXABI::EmitMemberPointerConversion() - Base virtual method
with NYI default, to be overridden by specific ABI implementations.
4. Implemented CIRGenItaniumCXXABI::EmitMemberPointerConversion() for the
Itanium C++ ABI:
- CK_ReinterpretMemberPointer: Returns source unchanged (no adjustment)
- CK_DerivedToBaseMemberPointer/CK_BaseToDerivedMemberPointer:
* For member data pointers: Adjusts the cir::DataMemberAttr index
by the offset, matching LLVM's nsw add/sub on constant integers
* Member function pointers: Marked NYI (requires struct {ptr, adj})
5. Updated ConstExprEmitter::VisitCastExpr() to call the new infrastructure
instead of llvm_unreachable().
Differences from CodeGen:
- Uses CIR's attribute system (cir::IntAttr, cir::DataMemberAttr) instead
of LLVM constants
- Member data pointer adjustment directly modifies the DataMemberAttr
index rather than operating on raw integer constants
Test Plan:
- Added clang/test/CIR/CodeGen/member-pointer-cast.cpp to verify CIR output
- Updated clang/test/CIR/crashes/constexpr-cast.cpp to reflect progress
(original NYI at line 1006 is fixed; now fails at member function pointer
emission from APValue, which is a separate feature)
The crash test case from TODO.md item #9 now progresses past the constant
expression cast NYI to a different unimplemented feature (member function
pointer emission), demonstrating that the core cast infrastructure works.
Fixes original NYI at CIRGenExprConst.cpp:1006
ghstack-source-id: cf00c7d
Pull-Request: #2017
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 implements support for member pointer casts in constant
expressions, specifically handling CK_DerivedToBaseMemberPointer,
CK_BaseToDerivedMemberPointer, and CK_ReinterpretMemberPointer cast kinds.
The implementation closely follows CodeGen's approach in CGExprConstant.cpp
and ItaniumCXXABI.cpp, adapted for CIR's attribute-based constant model.
Implementation details:
Added CIRGenModule::GetNonVirtualBaseClassOffset() - Computes the offset
between derived and base classes as a constant cir::IntAttr, mirroring
CodeGen's GetNonVirtualBaseClassOffset() which returns llvm::Constant.
Added CIRGenCXXABI::getMemberPointerAdjustment() - Calculates the
adjustment needed for member pointer conversions by determining the
derived type based on cast direction and delegating to
GetNonVirtualBaseClassOffset().
Added CIRGenCXXABI::EmitMemberPointerConversion() - Base virtual method
with NYI default, to be overridden by specific ABI implementations.
Implemented CIRGenItaniumCXXABI::EmitMemberPointerConversion() for the
Itanium C++ ABI:
by the offset, matching LLVM's nsw add/sub on constant integers
Updated ConstExprEmitter::VisitCastExpr() to call the new infrastructure
instead of llvm_unreachable().
Differences from CodeGen:
of LLVM constants
index rather than operating on raw integer constants
Test Plan:
(original NYI at line 1006 is fixed; now fails at member function pointer
emission from APValue, which is a separate feature)
The crash test case from TODO.md item #9 now progresses past the constant
expression cast NYI to a different unimplemented feature (member function
pointer emission), demonstrating that the core cast infrastructure works.
Fixes original NYI at CIRGenExprConst.cpp:1006