llvm / clangir

A new (MLIR based) high-level IR for clang.
https://clangir.org
Other
327 stars 86 forks source link

[CIR][CIRGen][LLVMLowering] Vtable support for simple multiple inhertance without thunk #569

Closed Laity000 closed 2 months ago

Laity000 commented 3 months ago

This PR adds Vtable support for C++ multiple inheritance without thunk. This change contains the CIR codegen and lowering work:

  1. VTableAttr should allow adding multiple ArrayAttr for multi-inheritance.
  2. VTableAddrPointOpLowering has been fixed for the multi-vtable during the MLIR lowering phase.

Example:

    class Mother {
      virtual void MotherFoo() {}
      virtual void MotherFoo2() {}
    }

    class Father {
      virtual void FatherFoo() {}
    }

    class Child : public Mother, public Father {
      void MotherFoo() override {}
    }
    cir.global linkonce_odr @_ZTV5Child = #cir.vtable<
    {#cir.const_array<[
      #cir.ptr<null> :  #!cir.ptr<!u8i>,
      #cir.global_view<@_ZTI5Child> : !cir.ptr<!u8i>,
      #cir.global_view<@_ZN5Child9MotherFooEv> : !cir.ptr<!u8i>,
      #cir.global_view<@_ZN6Mother10MotherFoo2Ev> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>,
     #cir.const_array<[
      #cir.ptr<-8> : !cir.ptr<!u8i>,
      #cir.global_view<@_ZTI5Child> : !cir.ptr<!u8i>,
      #cir.global_view<@_ZN6Father9FatherFooEv> : !cir.ptr<!u8i>]
    > : !cir.array<!cir.ptr<!u8i> x 3>}> : !ty_anon_struct3
Laity000 commented 3 months ago

I think we need some further discussions on the typing of vtables. CIRGen currently follows the original clang CodeGen scheme which represents vtables as arrays of pointers. Although most elements in a vtable are indeed pointers, elements such as offset-to-top are not. Representing these elements as pointers requires ugly hack as shown in this commit (i.e. #cir.ptr<-8>).

Thank you for your review.

I've reviewed the Itanium C++ ABI document:

Offsets are of type ptrdiff_t unless otherwise stated. We call it the address point of the virtual table. The virtual table may therefore contain components at either positive or negative offsets from its address point.

So, I'm not sure whether to use ptrdiff_t, ptr, or a new attr for the type of vtable?

This might require a further discussion. @Lancern @bcardosolopes

bcardosolopes commented 3 months ago

@lanza rebased over the weekend, sorry for the churn. Can you rebase this PR again?