[CIR] Implement APValue emission for member function pointers #2016
+202
−18
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 change implements APValue emission for member function pointers in
constexpr contexts, fixing crashes when encountering member function
pointers in constant expressions.
Problem:
Previously, attempting to emit a member function pointer in a constant
expression would hit an assertion at CIRGenExprConst.cpp:2075 with
"not implemented". This prevented compilation of valid C++ code that
uses member function pointers as compile-time constants.
Solution:
The implementation follows CodeGen's approach in CGExprConstant.cpp,
delegating to the CXX ABI for member pointer emission. Key changes:
Extended MethodAttr to include an 'adjustment' field for the
this-pointer adjustment needed for multiple inheritance and virtual
base classes, per Itanium C++ ABI 2.3.2 (member function pointers are
represented as struct { fnptr_t ptr; ptrdiff_t adj }).
Added CIRGenCXXABI interface methods:
Implemented these methods in CIRGenItaniumCXXABI:
Updated ConstantEmitter::tryEmitPrivate to delegate member pointer
emission to the ABI implementation, with proper llvm_unreachable
guard for NYI derived-to-base conversions
Updated lowering in ItaniumCXXABI.cpp to use the adjustment field
when lowering MethodAttr to LLVM IR
Added support in LowerToLLVM.cpp for lowering MethodAttr global
initializers by delegating to the ABI layer (matching the pattern
used for DataMemberAttr)
Testing (member-ptr-init.cpp):
and null member function pointers
Itanium ABI with function pointer/vtable offset and adjustment
CodeGen for all test cases
All 385 existing CIR CodeGen tests continue to pass.
Implementation follows ClangIR conventions: