Skip to content

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 24, 2025

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:

  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-poisoned]
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants