Skip to content

Commit

Permalink
[CIR] Add support for comparisons between pointers to member functions
Browse files Browse the repository at this point in the history
The CIRGen support is already there. This patch adds LLVM lowering support for
comparisons between pointers to member functions. Note that pointers to member
functions could only be compared for equality.
  • Loading branch information
Lancern committed Feb 25, 2025
1 parent dc932de commit 8ab1895
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
4 changes: 4 additions & 0 deletions clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRCXXABI.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class CIRCXXABI {
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const = 0;

virtual mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const = 0;

virtual mlir::Value
lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
mlir::Value loweredSrc,
Expand Down
59 changes: 59 additions & 0 deletions clang/lib/CIR/Dialect/Transforms/TargetLowering/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class ItaniumCXXABI : public CIRCXXABI {
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const override;

mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const override;

mlir::Value lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
Expand Down Expand Up @@ -478,6 +482,61 @@ mlir::Value ItaniumCXXABI::lowerDataMemberCmp(cir::CmpOp op,
loweredRhs);
}

mlir::Value ItaniumCXXABI::lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const {
assert(op.getKind() == cir::CmpOpKind::eq ||
op.getKind() == cir::CmpOpKind::ne);

cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(LM);
mlir::Value ptrdiffZero = builder.create<cir::ConstantOp>(
op.getLoc(), ptrdiffCIRTy, cir::IntAttr::get(ptrdiffCIRTy, 0));

mlir::Value lhsPtrField = builder.create<cir::ExtractMemberOp>(
op.getLoc(), ptrdiffCIRTy, loweredLhs, 0);
mlir::Value rhsPtrField = builder.create<cir::ExtractMemberOp>(
op.getLoc(), ptrdiffCIRTy, loweredRhs, 0);
mlir::Value ptrCmp = builder.create<cir::CmpOp>(op.getLoc(), op.getKind(),
lhsPtrField, rhsPtrField);
mlir::Value ptrCmpToNull = builder.create<cir::CmpOp>(
op.getLoc(), op.getKind(), lhsPtrField, ptrdiffZero);

mlir::Value lhsAdjField = builder.create<cir::ExtractMemberOp>(
op.getLoc(), ptrdiffCIRTy, loweredLhs, 1);
mlir::Value rhsAdjField = builder.create<cir::ExtractMemberOp>(
op.getLoc(), ptrdiffCIRTy, loweredRhs, 1);
mlir::Value adjCmp = builder.create<cir::CmpOp>(op.getLoc(), op.getKind(),
lhsAdjField, rhsAdjField);

// We use cir.select to represent "||" and "&&" operations below:
// - cir.select if %a then %b else false => %a && %b
// - cir.select if %a then true else %b => %a || %b
// TODO: Do we need to invent dedicated "cir.logical_or" and "cir.logical_and"
// operations for this?
auto boolTy = cir::BoolType::get(op.getContext());
mlir::Value trueValue = builder.create<cir::ConstantOp>(
op.getLoc(), boolTy, cir::BoolAttr::get(op.getContext(), boolTy, true));
mlir::Value falseValue = builder.create<cir::ConstantOp>(
op.getLoc(), boolTy, cir::BoolAttr::get(op.getContext(), boolTy, false));
auto create_and = [&](mlir::Value lhs, mlir::Value rhs) {
return builder.create<cir::SelectOp>(op.getLoc(), lhs, rhs, falseValue);
};
auto create_or = [&](mlir::Value lhs, mlir::Value rhs) {
return builder.create<cir::SelectOp>(op.getLoc(), lhs, trueValue, rhs);
};

mlir::Value result;
if (op.getKind() == cir::CmpOpKind::eq) {
// (lhs.ptr == null || lhs.adj == rhs.adj) && lhs.ptr == rhs.ptr
result = create_and(create_or(ptrCmpToNull, adjCmp), ptrCmp);
} else {
// (lhs.ptr != null && lhs.adj != rhs.adj) || lhs.ptr != rhs.ptr
result = create_or(create_and(ptrCmpToNull, adjCmp), ptrCmp);
}

return result;
}

mlir::Value
ItaniumCXXABI::lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
mlir::Value loweredSrc,
Expand Down
13 changes: 10 additions & 3 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,10 +2893,17 @@ mlir::LogicalResult CIRToLLVMCmpOpLowering::matchAndRewrite(
mlir::ConversionPatternRewriter &rewriter) const {
auto type = cmpOp.getLhs().getType();

if (mlir::isa<cir::DataMemberType>(type)) {
if (mlir::isa<cir::DataMemberType, cir::MethodType>(type)) {
assert(lowerMod && "lowering module is not available");
mlir::Value loweredResult = lowerMod->getCXXABI().lowerDataMemberCmp(
cmpOp, adaptor.getLhs(), adaptor.getRhs(), rewriter);

mlir::Value loweredResult;
if (mlir::isa<cir::DataMemberType>(type))
loweredResult = lowerMod->getCXXABI().lowerDataMemberCmp(
cmpOp, adaptor.getLhs(), adaptor.getRhs(), rewriter);
else
loweredResult = lowerMod->getCXXABI().lowerMethodCmp(
cmpOp, adaptor.getLhs(), adaptor.getRhs(), rewriter);

rewriter.replaceOp(cmpOp, loweredResult);
return mlir::success();
}
Expand Down
40 changes: 40 additions & 0 deletions clang/test/CIR/CodeGen/pointer-to-member-func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,43 @@ void call(Foo *obj, void (Foo::*func)(int), int arg) {
// LLVM-NEXT: %[[#arg:]] = load i32, ptr %{{.+}}
// LLVM-NEXT: call void %[[#callee_ptr]](ptr %[[#adjusted_this]], i32 %[[#arg]])
// LLVM: }

bool cmp_eq(void (Foo::*lhs)(int), void (Foo::*rhs)(int)) {
return lhs == rhs;
}

// CHECK-LABEL: @_Z6cmp_eqM3FooFviES1_
// CHECK: %{{.+}} = cir.cmp(eq, %{{.+}}, %{{.+}}) : !cir.method<!cir.func<(!s32i)> in !ty_Foo>, !cir.bool

// LLVM-LABEL: @_Z6cmp_eqM3FooFviES1_
// LLVM: %[[#lhs:]] = load { i64, i64 }, ptr %{{.+}}
// LLVM-NEXT: %[[#rhs:]] = load { i64, i64 }, ptr %{{.+}}
// LLVM-NEXT: %[[#lhs_ptr:]] = extractvalue { i64, i64 } %[[#lhs]], 0
// LLVM-NEXT: %[[#rhs_ptr:]] = extractvalue { i64, i64 } %[[#rhs]], 0
// LLVM-NEXT: %[[#ptr_cmp:]] = icmp eq i64 %[[#lhs_ptr]], %[[#rhs_ptr]]
// LLVM-NEXT: %[[#ptr_null:]] = icmp eq i64 %[[#lhs_ptr]], 0
// LLVM-NEXT: %[[#lhs_adj:]] = extractvalue { i64, i64 } %[[#lhs]], 1
// LLVM-NEXT: %[[#rhs_adj:]] = extractvalue { i64, i64 } %[[#rhs]], 1
// LLVM-NEXT: %[[#adj_cmp:]] = icmp eq i64 %[[#lhs_adj]], %[[#rhs_adj]]
// LLVM-NEXT: %[[#tmp:]] = or i1 %[[#ptr_null]], %[[#adj_cmp]]
// LLVM-NEXT: %{{.+}} = and i1 %[[#tmp]], %[[#ptr_cmp]]

bool cmp_ne(void (Foo::*lhs)(int), void (Foo::*rhs)(int)) {
return lhs != rhs;
}

// CHECK-LABEL: @_Z6cmp_neM3FooFviES1_
// CHECK: %{{.+}} = cir.cmp(ne, %{{.+}}, %{{.+}}) : !cir.method<!cir.func<(!s32i)> in !ty_Foo>, !cir.bool

// LLVM-LABEL: @_Z6cmp_neM3FooFviES1_
// LLVM: %[[#lhs:]] = load { i64, i64 }, ptr %{{.+}}
// LLVM-NEXT: %[[#rhs:]] = load { i64, i64 }, ptr %{{.+}}
// LLVM-NEXT: %[[#lhs_ptr:]] = extractvalue { i64, i64 } %[[#lhs]], 0
// LLVM-NEXT: %[[#rhs_ptr:]] = extractvalue { i64, i64 } %[[#rhs]], 0
// LLVM-NEXT: %[[#ptr_cmp:]] = icmp ne i64 %[[#lhs_ptr]], %[[#rhs_ptr]]
// LLVM-NEXT: %[[#ptr_null:]] = icmp ne i64 %[[#lhs_ptr]], 0
// LLVM-NEXT: %[[#lhs_adj:]] = extractvalue { i64, i64 } %[[#lhs]], 1
// LLVM-NEXT: %[[#rhs_adj:]] = extractvalue { i64, i64 } %[[#rhs]], 1
// LLVM-NEXT: %[[#adj_cmp:]] = icmp ne i64 %[[#lhs_adj]], %[[#rhs_adj]]
// LLVM-NEXT: %[[#tmp:]] = and i1 %[[#ptr_null]], %[[#adj_cmp]]
// LLVM-NEXT: %{{.+}} = or i1 %[[#tmp]], %[[#ptr_cmp]]

0 comments on commit 8ab1895

Please sign in to comment.