[clang] [CIR] Add support for accessing members of base classes (PR #143195)
Bruno Cardoso Lopes via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 9 06:22:57 PDT 2025
================
@@ -2231,4 +2231,48 @@ def VecTernaryOp : CIR_Op<"vec.ternary",
let hasVerifier = 1;
}
+//===----------------------------------------------------------------------===//
+// BaseClassAddrOp
+//===----------------------------------------------------------------------===//
+
+def BaseClassAddrOp : CIR_Op<"base_class_addr"> {
+ let summary = "Get the base class address for a class/struct";
+ let description = [{
+ The `cir.base_class_addr` operaration gets the address of a particular
+ non-virtual base class given a derived class pointer. The offset in bytes
+ of the base class must be passed in, since it is easier for the front end
+ to calculate that than the MLIR passes. The operation contains a flag for
+ whether or not the operand may be nullptr. That depends on the context and
+ cannot be known by the operation, and that information affects how the
+ operation is lowered.
+
+ Example:
+ ```c++
+ struct Base { };
+ struct Derived : Base { };
+ Derived d;
+ Base& b = d;
+ ```
+ will generate
+ ```mlir
+ %3 = cir.base_class_addr (%1 : !cir.ptr<!rec_Derived> nonnull) [0] -> !cir.ptr<!rec_Base>
+ ```
+ }];
+
+ // The validity of the relationship of derived and base cannot yet be
+ // verified, currently not worth adding a verifier.
+ let arguments = (ins
+ Arg<CIR_PointerType, "derived class pointer", [MemRead]>:$derived_addr,
+ IndexAttr:$offset, UnitAttr:$assume_not_null);
+
+ let results = (outs Res<CIR_PointerType, "">:$base_addr);
+
+ let assemblyFormat = [{
+ `(`
----------------
bcardosolopes wrote:
This could be a good opportunity to get rid of the `(` and `)` (https://llvm.github.io/clangir/Dialect/cir-style-guide.html).
https://github.com/llvm/llvm-project/pull/143195
More information about the cfe-commits
mailing list