Skip to content

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 23, 2025

Stack from ghstack (oldest at bottom):

This commit implements runtime type identification for polymorphic types,
completing typeid operator support in ClangIR.

Previously, only non-polymorphic (static) typeid was supported. This adds:

  1. Polymorphic typeid emission: When typeid is applied to an expression
    of polymorphic class type, we now emit vtable lookup to get the runtime
    type_info pointer.

  2. Null pointer checking: For pointer operands, we emit null checks and
    call emitBadTypeidCall (currently unreachable until exception support).
    Matches CodeGen behavior of always checking, even for references.

  3. Type safety checks: emitTypeCheck ensures typeid is not used unsafely
    during object construction or destruction.

  4. Vtable layouts: Supports both traditional (vtable[-1]) and relative
    (load.relative(vtable, -4)) layouts for Itanium ABI.

Implementation follows CodeGen closely:

  • emitTypeidFromVTable handles the main logic with null checking
  • CIRGenItaniumCXXABI::emitTypeid handles ABI-specific vtable access
  • Uses existing getVTablePtr infrastructure
  • shouldTypeidBeNullChecked matches CodeGen (always returns true)

The vtable layout for type_info access:

  • Absolute: type_info* at vtable[-1] (8 bytes before vptr)
  • Relative: type_info offset at vtable[-4] (4 bytes before vptr)

Test Plan:

  • Added typeid-polymorphic.cpp with comprehensive tests
  • Tests verify CIR output, LLVM lowering, and CodeGen comparison (OGCG)
  • All 4 test cases have CIR + LLVM + OGCG checks
  • Covers: basic usage, references, derived classes, const pointers
  • Note: CIR lowering of PtrStrideOp does not emit 'inbounds' flag
    (general CIR lowering issue, not specific to this feature)
  • All CIR tests pass

[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
This commit implements runtime type identification for polymorphic types,
completing typeid operator support in ClangIR.

Previously, only non-polymorphic (static) typeid was supported. This adds:

1. **Polymorphic typeid emission**: When typeid is applied to an expression
   of polymorphic class type, we now emit vtable lookup to get the runtime
   type_info pointer.

2. **Null pointer checking**: For pointer operands, we emit null checks and
   call emitBadTypeidCall (currently unreachable until exception support).
   Matches CodeGen behavior of always checking, even for references.

3. **Type safety checks**: emitTypeCheck ensures typeid is not used unsafely
   during object construction or destruction.

4. **Vtable layouts**: Supports both traditional (vtable[-1]) and relative
   (load.relative(vtable, -4)) layouts for Itanium ABI.

Implementation follows CodeGen closely:
- emitTypeidFromVTable handles the main logic with null checking
- CIRGenItaniumCXXABI::emitTypeid handles ABI-specific vtable access
- Uses existing getVTablePtr infrastructure
- shouldTypeidBeNullChecked matches CodeGen (always returns true)

The vtable layout for type_info access:
- Absolute: type_info* at vtable[-1] (8 bytes before vptr)
- Relative: type_info offset at vtable[-4] (4 bytes before vptr)

Test Plan:
- Added typeid-polymorphic.cpp with comprehensive tests
- Tests verify CIR output, LLVM lowering, and CodeGen comparison (OGCG)
- All 4 test cases have CIR + LLVM + OGCG checks
- Covers: basic usage, references, derived classes, const pointers
- Note: CIR lowering of PtrStrideOp does not emit 'inbounds' flag
  (general CIR lowering issue, not specific to this feature)
- All CIR tests pass


ghstack-source-id: 57a74ba
Pull-Request: #2005
@lanza lanza closed this Nov 24, 2025
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