Skip to content
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

[CIR][NFC] Fix bug during fp16 unary op CIRGen #706

Merged
merged 1 commit into from
Jul 1, 2024
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
42 changes: 26 additions & 16 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,37 +604,47 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
: PromotionType;
auto result = VisitPlus(E, promotionTy);
if (result && !promotionTy.isNull())
result = buildUnPromotedValue(result, E->getType());
return buildUnaryOp(E, mlir::cir::UnaryOpKind::Plus, result);
return buildUnPromotedValue(result, E->getType());
return result;
}

mlir::Value VisitPlus(const UnaryOperator *E, QualType PromotionType) {
mlir::Value VisitPlus(const UnaryOperator *E,
QualType PromotionType = QualType()) {
// This differs from gcc, though, most likely due to a bug in gcc.
TestAndClearIgnoreResultAssign();

mlir::Value operand;
if (!PromotionType.isNull())
return CGF.buildPromotedScalarExpr(E->getSubExpr(), PromotionType);
return Visit(E->getSubExpr());
operand = CGF.buildPromotedScalarExpr(E->getSubExpr(), PromotionType);
else
operand = Visit(E->getSubExpr());

return buildUnaryOp(E, mlir::cir::UnaryOpKind::Plus, operand);
}

mlir::Value VisitUnaryMinus(const UnaryOperator *E) {
// NOTE(cir): QualType function parameter still not used, so don´t replicate
// it here yet.
QualType promotionTy = getPromotionType(E->getSubExpr()->getType());
mlir::Value VisitUnaryMinus(const UnaryOperator *E,
QualType PromotionType = QualType()) {
QualType promotionTy = PromotionType.isNull()
? getPromotionType(E->getSubExpr()->getType())
: PromotionType;
auto result = VisitMinus(E, promotionTy);
if (result && !promotionTy.isNull())
result = buildUnPromotedValue(result, E->getType());
return buildUnaryOp(E, mlir::cir::UnaryOpKind::Minus, result);
return buildUnPromotedValue(result, E->getType());
return result;
}

mlir::Value VisitMinus(const UnaryOperator *E, QualType PromotionType) {
TestAndClearIgnoreResultAssign();

mlir::Value operand;
if (!PromotionType.isNull())
return CGF.buildPromotedScalarExpr(E->getSubExpr(), PromotionType);
operand = CGF.buildPromotedScalarExpr(E->getSubExpr(), PromotionType);
else
operand = Visit(E->getSubExpr());

// NOTE: LLVM codegen will lower this directly to either a FNeg
// or a Sub instruction. In CIR this will be handled later in LowerToLLVM.

return Visit(E->getSubExpr());
return buildUnaryOp(E, mlir::cir::UnaryOpKind::Minus, operand);
}

mlir::Value VisitUnaryNot(const UnaryOperator *E) {
Expand All @@ -660,8 +670,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
mlir::Value buildUnaryOp(const UnaryOperator *E, mlir::cir::UnaryOpKind kind,
mlir::Value input) {
return Builder.create<mlir::cir::UnaryOp>(
CGF.getLoc(E->getSourceRange().getBegin()),
CGF.getCIRType(E->getType()), kind, input);
CGF.getLoc(E->getSourceRange().getBegin()), input.getType(), kind,
input);
}

// C++
Expand Down
8 changes: 4 additions & 4 deletions clang/test/CIR/CodeGen/bf16-ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ void foo(void) {

h1 = -h1;
// NONATIVE: %[[#A:]] = cir.cast(floating, %{{.+}} : !cir.bf16), !cir.float
// NONATIVE-NEXT: %[[#B:]] = cir.cast(floating, %[[#A]] : !cir.float), !cir.bf16
// NONATIVE-NEXT: %{{.+}} = cir.unary(minus, %[[#B]]) : !cir.bf16, !cir.bf16
// NONATIVE-NEXT: %[[#B:]] = cir.unary(minus, %[[#A]]) : !cir.float, !cir.float
// NONATIVE-NEXT: %{{.+}} = cir.cast(floating, %[[#B]] : !cir.float), !cir.bf16

// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.bf16), !cir.float
// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.float), !cir.bf16
// NATIVE: %{{.+}} = cir.unary(minus, %{{.+}}) : !cir.bf16, !cir.bf16

h1 = +h1;
// NONATIVE: %[[#A:]] = cir.cast(floating, %{{.+}} : !cir.bf16), !cir.float
// NONATIVE-NEXT: %[[#B:]] = cir.cast(floating, %[[#A]] : !cir.float), !cir.bf16
// NONATIVE-NEXT: %{{.+}} = cir.unary(plus, %[[#B]]) : !cir.bf16, !cir.bf16
// NONATIVE-NEXT: %[[#B:]] = cir.unary(plus, %[[#A]]) : !cir.float, !cir.float
// NONATIVE-NEXT: %{{.+}} = cir.cast(floating, %[[#B]] : !cir.float), !cir.bf16

// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.bf16), !cir.float
// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.float), !cir.bf16
Expand Down
8 changes: 4 additions & 4 deletions clang/test/CIR/CodeGen/fp16-ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ void foo(void) {

h1 = -h1;
// NONATIVE: %[[#A:]] = cir.cast(floating, %{{.+}} : !cir.f16), !cir.float
// NONATIVE-NEXT: %[[#B:]] = cir.cast(floating, %[[#A]] : !cir.float), !cir.f16
// NONATIVE-NEXT: %{{.+}} = cir.unary(minus, %[[#B]]) : !cir.f16, !cir.f16
// NONATIVE-NEXT: %[[#B:]] = cir.unary(minus, %[[#A]]) : !cir.float, !cir.float
// NONATIVE-NEXT: %{{.+}} = cir.cast(floating, %[[#B]] : !cir.float), !cir.f16

// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.f16), !cir.float
// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.float), !cir.f16
// NATIVE: %{{.+}} = cir.unary(minus, %{{.+}}) : !cir.f16, !cir.f16

h1 = +h1;
// NONATIVE: %[[#A:]] = cir.cast(floating, %{{.+}} : !cir.f16), !cir.float
// NONATIVE-NEXT: %[[#B:]] = cir.cast(floating, %[[#A]] : !cir.float), !cir.f16
// NONATIVE-NEXT: %{{.+}} = cir.unary(plus, %[[#B]]) : !cir.f16, !cir.f16
// NONATIVE-NEXT: %[[#B:]] = cir.unary(plus, %[[#A]]) : !cir.float, !cir.float
// NONATIVE-NEXT: %{{.+}} = cir.cast(floating, %[[#B]] : !cir.float), !cir.f16

// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.f16), !cir.float
// NATIVE-NOT: %{{.+}} = cir.cast(floating, %{{.+}} : !cir.float), !cir.f16
Expand Down
Loading