Skip to content

[CIR] LLVM lowering support for pointers to member functions #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,16 @@ def MethodAttr : CIR_Attr<"Method", "method", [TypedAttrInterface]> {
let hasCustomAssemblyFormat = 1;

let genVerifyDecl = 1;

let extraClassDeclaration = [{
bool isNull() const {
return !getSymbol().has_value() && !getVtableOffset().has_value();
}

bool isVirtual() const {
return getVtableOffset().has_value();
}
}];
}

//===----------------------------------------------------------------------===//
Expand Down
58 changes: 58 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,64 @@ def GetMemberOp : CIR_Op<"get_member"> {
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// ExtractMemberOp
//===----------------------------------------------------------------------===//

def ExtractMemberOp : CIR_Op<"extract_member", [Pure]> {
let summary = "Extract the value of a member of a struct value";
let description = [{
The `cir.extract_member` operation extracts the value of a particular member
from the input record. Unlike `cir.get_member` which derives pointers, this
operation operates on values. It takes a value of record type, and extract
the value of the specified record member from the input record value.

Currently `cir.extract_member` does not work on unions.

Example:

```mlir
// Suppose we have a struct with multiple members.
!s32i = !cir.int<s, 32>
!s8i = !cir.int<s, 32>
!struct_ty = !cir.struct<"struct.Bar" {!s32i, !s8i}>

// And suppose we have a value of the struct type.
%0 = cir.const #cir.const_struct<{#cir.int<1> : !s32i, #cir.int<2> : !s8i}> : !struct_ty

// Extract the value of the second member of the struct.
%1 = cir.extract_member %0[1] : !struct_ty -> !s8i
```
}];

let arguments = (ins CIR_StructType:$record, IndexAttr:$index_attr);
let results = (outs CIR_AnyType:$result);

let assemblyFormat = [{
$record `[` $index_attr `]` attr-dict
`:` qualified(type($record)) `->` qualified(type($result))
}];

let builders = [
OpBuilder<(ins "mlir::Type":$type, "mlir::Value":$record, "uint64_t":$index), [{
mlir::APInt fieldIdx(64, index);
build($_builder, $_state, type, record, fieldIdx);
}]>,
OpBuilder<(ins "mlir::Value":$record, "uint64_t":$index), [{
auto recordTy = mlir::cast<cir::StructType>(record.getType());
mlir::Type memberTy = recordTy.getMembers()[index];
build($_builder, $_state, memberTy, record, index);
}]>
];

let extraClassDeclaration = [{
/// Get the index of the struct member being accessed.
uint64_t getIndex() { return getIndexAttr().getZExtValue(); }
}];

let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// GetRuntimeMemberOp
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ struct MissingFeatures {
static bool tbaaPointer() { return false; }
static bool emitNullabilityCheck() { return false; }
static bool ptrAuth() { return false; }
static bool emitCFICheck() { return false; }
static bool emitVFEInfo() { return false; }
static bool emitWPDInfo() { return false; }

// GNU vectors are done, but other kinds of vectors haven't been implemented.
static bool scalableVectors() { return false; }
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3577,6 +3577,22 @@ LogicalResult cir::GetMemberOp::verify() {
return mlir::success();
}

//===----------------------------------------------------------------------===//
// ExtractMemberOp Definitions
//===----------------------------------------------------------------------===//

LogicalResult cir::ExtractMemberOp::verify() {
auto recordTy = mlir::cast<cir::StructType>(getRecord().getType());
if (recordTy.getKind() == cir::StructType::Union)
return emitError()
<< "cir.extract_member currently does not work on unions";
if (recordTy.getMembers().size() <= getIndex())
return emitError() << "member index out of bounds";
if (recordTy.getMembers()[getIndex()] != getType())
return emitError() << "member type mismatch";
return mlir::success();
}

//===----------------------------------------------------------------------===//
// GetRuntimeMemberOp Definitions
//===----------------------------------------------------------------------===//
Expand Down
20 changes: 20 additions & 0 deletions clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,40 @@ class CIRCXXABI {
lowerDataMemberType(cir::DataMemberType type,
const mlir::TypeConverter &typeConverter) const = 0;

/// Lower the given member function pointer type to its ABI type. The returned
/// type is also a CIR type.
virtual mlir::Type
lowerMethodType(cir::MethodType type,
const mlir::TypeConverter &typeConverter) const = 0;

/// Lower the given data member pointer constant to a constant of the ABI
/// type. The returned constant is represented as an attribute as well.
virtual mlir::TypedAttr
lowerDataMemberConstant(cir::DataMemberAttr attr,
const mlir::DataLayout &layout,
const mlir::TypeConverter &typeConverter) const = 0;

/// Lower the given member function pointer constant to a constant of the ABI
/// type. The returned constant is represented as an attribute as well.
virtual mlir::TypedAttr
lowerMethodConstant(cir::MethodAttr attr, const mlir::DataLayout &layout,
const mlir::TypeConverter &typeConverter) const = 0;

/// Lower the given cir.get_runtime_member op to a sequence of more
/// "primitive" CIR operations that act on the ABI types.
virtual mlir::Operation *
lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
mlir::Value loweredAddr, mlir::Value loweredMember,
mlir::OpBuilder &builder) const = 0;

/// Lower the given cir.get_method op to a sequence of more "primitive" CIR
/// operations that act on the ABI types. The lowered result values will be
/// stored in the given loweredResults array.
virtual void
lowerGetMethod(cir::GetMethodOp op, mlir::Value (&loweredResults)[2],
mlir::Value loweredMethod, mlir::Value loweredObjectPtr,
mlir::ConversionPatternRewriter &rewriter) const = 0;

/// Lower the given cir.base_data_member op to a sequence of more "primitive"
/// CIR operations that act on the ABI types.
virtual mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op,
Expand Down
Loading
Loading