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][CodeGen] Basic support for unary plus and minus #20

Merged
merged 3 commits into from
Oct 18, 2022
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
12 changes: 8 additions & 4 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,18 @@ def ScopeOp : CIR_Op<"scope", [DeclareOpInterfaceMethods<RegionBranchOpInterface
// UnaryOp
//===----------------------------------------------------------------------===//

def UnaryOpKind_Inc : I32EnumAttrCase<"Inc", 1, "inc">;
def UnaryOpKind_Dec : I32EnumAttrCase<"Dec", 2, "dec">;
def UnaryOpKind_Inc : I32EnumAttrCase<"Inc", 1, "inc">;
def UnaryOpKind_Dec : I32EnumAttrCase<"Dec", 2, "dec">;
def UnaryOpKind_Plus : I32EnumAttrCase<"Plus", 3, "plus">;
def UnaryOpKind_Minus : I32EnumAttrCase<"Minus", 4, "minus">;

def UnaryOpKind : I32EnumAttr<
"UnaryOpKind",
"unary operation kind",
[UnaryOpKind_Inc,
UnaryOpKind_Dec]> {
UnaryOpKind_Dec,
UnaryOpKind_Plus,
UnaryOpKind_Minus]> {
let cppNamespace = "::mlir::cir";
}

Expand All @@ -561,7 +565,7 @@ def UnaryOp : CIR_Op<"unary",
let summary = "Unary operations";
let description = [{
`cir.unary` performs the unary operation according to
the specified opcode kind: [inc, dec].
the specified opcode kind: [inc, dec, plus, minus].

Note for inc and dec: the operation corresponds only to the
addition/subtraction, its input is expect to come from a load
Expand Down
53 changes: 51 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,44 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
llvm_unreachable("NYI");
}
mlir::Value VisitUnaryPlus(const UnaryOperator *E) {
llvm_unreachable("NYI");
// NOTE(cir): QualType function parameter still not used, so don´t replicate
// it here yet.
QualType promotionTy = getPromotionType(E->getSubExpr()->getType());
auto result = VisitPlus(E, promotionTy);
if (result && !promotionTy.isNull())
assert(0 && "not implemented yet");
return buildUnaryOp(E, mlir::cir::UnaryOpKind::Plus, result);
}

mlir::Value VisitPlus(const UnaryOperator *E, QualType PromotionType) {
// This differs from gcc, though, most likely due to a bug in gcc.
TestAndClearIgnoreResultAssign();
if (!PromotionType.isNull())
assert(0 && "scalar promotion not implemented yet");
return Visit(E->getSubExpr());
}

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

mlir::Value VisitMinus(const UnaryOperator *E, QualType PromotionType) {
TestAndClearIgnoreResultAssign();
if (!PromotionType.isNull())
assert(0 && "scalar promotion not implemented yet");

// 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also be worth a short comment explaining that during LLVM codegen float uses fneg and a emit sub is used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

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

but removed the comments I had in VisitUnaryPlus and VisitUnaryMinus.

}

mlir::Value VisitUnaryNot(const UnaryOperator *E) { llvm_unreachable("NYI"); }
mlir::Value VisitUnaryLNot(const UnaryOperator *E) {
llvm_unreachable("NYI");
Expand Down Expand Up @@ -592,6 +625,22 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
buildCompoundAssign(const CompoundAssignOperator *E,
mlir::Value (ScalarExprEmitter::*F)(const BinOpInfo &));

// TODO(cir): Candidate to be in a common AST helper between CIR and LLVM codegen.
QualType getPromotionType(QualType Ty) {
if (CGF.getContext()
.getTargetInfo()
.shouldEmitFloat16WithExcessPrecision()) {
if (Ty->isAnyComplexType()) {
QualType ElementType = Ty->castAs<ComplexType>()->getElementType();
if (ElementType->isFloat16Type())
return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
}
if (Ty->isFloat16Type())
return CGF.getContext().FloatTy;
}
return QualType();
}

// Binary operators and binary compound assignment operators.
#define HANDLEBINOP(OP) \
mlir::Value VisitBin##OP(const BinaryOperator *E) { \
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/CIR/CodeGen/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ class CIRUnaryOpLowering : public mlir::OpRewritePattern<mlir::cir::UnaryOp> {
op.getInput(), One);
break;
}
case mlir::cir::UnaryOpKind::Plus: {
rewriter.replaceOp(op, op.getInput());
break;
}
case mlir::cir::UnaryOpKind::Minus: {
auto Zero = rewriter.create<mlir::arith::ConstantOp>(
op.getLoc(), type, mlir::IntegerAttr::get(type, 0));
rewriter.replaceOpWithNewOp<mlir::arith::SubIOp>(op, op.getType(), Zero,
op.getInput());
break;
}
}

return mlir::LogicalResult::success();
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,12 @@ LogicalResult UnaryOp::verify() {
return emitOpError() << "requires result to be used by a memory store "
"to the same address as the input memory load";
}
case cir::UnaryOpKind::Plus:
// Nothing to verify.
return success();
case cir::UnaryOpKind::Minus:
// Nothing to verify.
return success();
}

llvm_unreachable("Unknown UnaryOp kind?");
Expand Down
29 changes: 29 additions & 0 deletions clang/test/CIR/CIRToLLVM/unary-plus-minus.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: cir-tool %s -cir-to-func -cir-to-memref -o - | FileCheck %s -check-prefix=MLIR
// RUN: cir-tool %s -cir-to-func -cir-to-memref -cir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM

module {
cir.func @foo() {
%0 = cir.alloca i32, cir.ptr <i32>, ["a", init] {alignment = 4 : i64}
%1 = cir.alloca i32, cir.ptr <i32>, ["b", init] {alignment = 4 : i64}
%2 = cir.cst(2 : i32) : i32
cir.store %2, %0 : i32, cir.ptr <i32>
cir.store %2, %1 : i32, cir.ptr <i32>

%3 = cir.load %0 : cir.ptr <i32>, i32
%4 = cir.unary(plus, %3) : i32, i32
cir.store %4, %0 : i32, cir.ptr <i32>

%5 = cir.load %1 : cir.ptr <i32>, i32
%6 = cir.unary(minus, %5) : i32, i32
cir.store %6, %1 : i32, cir.ptr <i32>
cir.return
}
}

// MLIR: %[[#INPUT_PLUS:]] = memref.load
// MLIR: memref.store %[[#INPUT_PLUS]]
// MLIR: %[[#INPUT_MINUS:]] = memref.load
// MLIR: %[[ZERO:[a-z0-9_]+]] = arith.constant 0
// MLIR: arith.subi %[[ZERO]], %[[#INPUT_MINUS]]

// LLVM: = sub i32 0, %[[#]]
26 changes: 26 additions & 0 deletions clang/test/CIR/CodeGen/unary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -Wno-unused-value -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

unsigned up0() {
unsigned a = 1;
return +a;
}

// CHECK: cir.func @_Z3up0v() -> i32 {
// CHECK: %[[#RET:]] = cir.alloca i32, cir.ptr <i32>, ["__retval"]
// CHECK: %[[#A:]] = cir.alloca i32, cir.ptr <i32>, ["a", init]
// CHECK: %[[#INPUT:]] = cir.load %[[#A]]
// CHECK: %[[#OUTPUT:]] = cir.unary(plus, %[[#INPUT]])
// CHECK: cir.store %[[#OUTPUT]], %[[#RET]]

unsigned um0() {
unsigned a = 1;
return -a;
}

// CHECK: cir.func @_Z3um0v() -> i32 {
// CHECK: %[[#RET:]] = cir.alloca i32, cir.ptr <i32>, ["__retval"]
// CHECK: %[[#A:]] = cir.alloca i32, cir.ptr <i32>, ["a", init]
// CHECK: %[[#INPUT:]] = cir.load %[[#A]]
// CHECK: %[[#OUTPUT:]] = cir.unary(minus, %[[#INPUT]])
// CHECK: cir.store %[[#OUTPUT]], %[[#RET]]