Skip to content
Open
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
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 @@ -53,6 +53,10 @@ class CIRCXXABI {
lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
mlir::Value loweredAddr, mlir::Value loweredMember,
mlir::OpBuilder &builder) const = 0;

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

/// Creates an Itanium-family ABI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ add_clang_library(MLIRCIRTargetLowering
MLIRDLTIDialect
MLIRCIR
MLIRCIRInterfaces
MLIRLLVMDialect
)
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class LowerItaniumCXXABI : public CIRCXXABI {
lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
mlir::Value loweredAddr, mlir::Value loweredMember,
mlir::OpBuilder &builder) const override;

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

} // namespace
Expand Down Expand Up @@ -108,4 +112,17 @@ mlir::Operation *LowerItaniumCXXABI::lowerGetRuntimeMember(
cir::CastKind::bitcast, memberBytesPtr);
}

mlir::Value
LowerItaniumCXXABI::lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const {
assert(op.getKind() == cir::CmpOpKind::eq ||
op.getKind() == cir::CmpOpKind::ne);
mlir::LLVM::ICmpPredicate kind = op.getKind() == cir::CmpOpKind::eq
? mlir::LLVM::ICmpPredicate::eq
: mlir::LLVM::ICmpPredicate::ne;
return mlir::LLVM::ICmpOp::create(builder, op.getLoc(), kind, loweredLhs,
loweredRhs);
}

} // namespace cir
11 changes: 10 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2559,8 +2559,17 @@ mlir::LogicalResult CIRToLLVMCmpOpLowering::matchAndRewrite(
mlir::ConversionPatternRewriter &rewriter) const {
mlir::Type type = cmpOp.getLhs().getType();

assert(!cir::MissingFeatures::dataMemberType());
assert(!cir::MissingFeatures::methodType());
if (mlir::isa<cir::DataMemberType>(type)) {
assert(lowerMod && "lowering module is not available");

mlir::Value loweredResult;
loweredResult = lowerMod->getCXXABI().lowerDataMemberCmp(
cmpOp, adaptor.getLhs(), adaptor.getRhs(), rewriter);

rewriter.replaceOp(cmpOp, loweredResult);
return mlir::success();
}

if (mlir::isa<cir::IntType, mlir::IntegerType>(type)) {
bool isSigned = mlir::isa<cir::IntType>(type)
Expand Down
58 changes: 58 additions & 0 deletions clang/test/CIR/CodeGen/pointer-to-data-member-cmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir --check-prefix=CIR %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --input-file=%t-cir.ll --check-prefix=LLVM %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll --check-prefix=OGCG %s

struct Foo {
int a;
};

struct Bar {
int a;
};

bool eq(int Foo::*x, int Foo::*y) {
return x == y;
}

// CIR-LABEL: @_Z2eqM3FooiS0_
// CIR: %[[#x:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.data_member<!s32i in !rec_Foo>>, !cir.data_member<!s32i in !rec_Foo>
// CIR-NEXT: %[[#y:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.data_member<!s32i in !rec_Foo>>, !cir.data_member<!s32i in !rec_Foo>
// CIR-NEXT: %{{.+}} = cir.cmp(eq, %[[#x]], %[[#y]]) : !cir.data_member<!s32i in !rec_Foo>, !cir.bool
// CIR: }

// LLVM-LABEL: @_Z2eqM3FooiS0_
// LLVM: %[[#x:]] = load i64, ptr %{{.+}}, align 8
// LLVM-NEXT: %[[#y:]] = load i64, ptr %{{.+}}, align 8
// LLVM-NEXT: %{{.+}} = icmp eq i64 %[[#x]], %[[#y]]
// LLVM: }

// OGCG-LABEL: @_Z2eqM3FooiS0_
// OGCG: %[[#x:]] = load i64, ptr %{{.+}}, align 8
// OGCG-NEXT: %[[#y:]] = load i64, ptr %{{.+}}, align 8
// OGCG-NEXT: %{{.+}} = icmp eq i64 %[[#x]], %[[#y]]
// OGCG: }

bool ne(int Foo::*x, int Foo::*y) {
return x != y;
}

// CIR-LABEL: @_Z2neM3FooiS0_
// CIR: %[[#x:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.data_member<!s32i in !rec_Foo>>, !cir.data_member<!s32i in !rec_Foo>
// CIR-NEXT: %[[#y:]] = cir.load{{.*}} %{{.+}} : !cir.ptr<!cir.data_member<!s32i in !rec_Foo>>, !cir.data_member<!s32i in !rec_Foo>
// CIR-NEXT: %{{.+}} = cir.cmp(ne, %[[#x]], %[[#y]]) : !cir.data_member<!s32i in !rec_Foo>, !cir.bool
// CIR: }

// LLVM-LABEL: @_Z2neM3FooiS0_
// LLVM: %[[#x:]] = load i64, ptr %{{.+}}, align 8
// LLVM-NEXT: %[[#y:]] = load i64, ptr %{{.+}}, align 8
// LLVM-NEXT: %{{.+}} = icmp ne i64 %[[#x]], %[[#y]]
// LLVM: }

// OGCG-LABEL: @_Z2neM3FooiS0_
// OGCG: %[[#x:]] = load i64, ptr %{{.+}}, align 8
// OGCG-NEXT: %[[#y:]] = load i64, ptr %{{.+}}, align 8
// OGCG-NEXT: %{{.+}} = icmp ne i64 %[[#x]], %[[#y]]
// OGCG: }
Loading