-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[CIR] Upstream unary not for ComplexType #148857
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,9 @@ | |||||||||||
|
|
||||||||||||
| #include "PassDetail.h" | ||||||||||||
| #include "clang/AST/ASTContext.h" | ||||||||||||
| #include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" | ||||||||||||
| #include "clang/CIR/Dialect/IR/CIRDialect.h" | ||||||||||||
| #include "clang/CIR/Dialect/IR/CIROpsEnums.h" | ||||||||||||
| #include "clang/CIR/Dialect/Passes.h" | ||||||||||||
|
|
||||||||||||
| #include <memory> | ||||||||||||
|
|
@@ -22,15 +24,68 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> { | |||||||||||
| void runOnOperation() override; | ||||||||||||
|
|
||||||||||||
| void runOnOp(Operation *op); | ||||||||||||
| void lowerUnaryOp(UnaryOp op); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| } // namespace | ||||||||||||
|
|
||||||||||||
| void LoweringPreparePass::runOnOp(Operation *op) {} | ||||||||||||
| void LoweringPreparePass::lowerUnaryOp(UnaryOp op) { | ||||||||||||
| mlir::Type ty = op.getType(); | ||||||||||||
| if (!mlir::isa<cir::ComplexType>(ty)) | ||||||||||||
| return; | ||||||||||||
|
|
||||||||||||
| mlir::Location loc = op.getLoc(); | ||||||||||||
| cir::UnaryOpKind opKind = op.getKind(); | ||||||||||||
|
|
||||||||||||
| CIRBaseBuilderTy builder(getContext()); | ||||||||||||
| builder.setInsertionPointAfter(op); | ||||||||||||
|
|
||||||||||||
| mlir::Value operand = op.getInput(); | ||||||||||||
| mlir::Value operandReal = builder.createComplexReal(loc, operand); | ||||||||||||
| mlir::Value operandImag = builder.createComplexImag(loc, operand); | ||||||||||||
|
|
||||||||||||
| mlir::Value resultReal; | ||||||||||||
| mlir::Value resultImag; | ||||||||||||
|
|
||||||||||||
| switch (opKind) { | ||||||||||||
| case cir::UnaryOpKind::Inc: | ||||||||||||
| case cir::UnaryOpKind::Dec: | ||||||||||||
| llvm_unreachable("Complex unary Inc/Dec NYI"); | ||||||||||||
| break; | ||||||||||||
|
|
||||||||||||
| case cir::UnaryOpKind::Plus: | ||||||||||||
| case cir::UnaryOpKind::Minus: | ||||||||||||
| llvm_unreachable("Complex unary Plus/Minus NYI"); | ||||||||||||
| break; | ||||||||||||
|
|
||||||||||||
| case cir::UnaryOpKind::Not: | ||||||||||||
| resultReal = operandReal; | ||||||||||||
| resultImag = | ||||||||||||
| builder.createUnaryOp(loc, cir::UnaryOpKind::Minus, operandImag); | ||||||||||||
| break; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| auto result = builder.createComplexCreate(loc, resultReal, resultImag); | ||||||||||||
| op.replaceAllUsesWith(result); | ||||||||||||
| op.erase(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| void LoweringPreparePass::runOnOp(Operation *op) { | ||||||||||||
|
||||||||||||
| void LoweringPreparePass::runOnOp(Operation *op) { | |
| void LoweringPreparePass::runOnOp(mlir::Operation *op) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (auto unary = dyn_cast<UnaryOp>(op)) { | |
| lowerUnaryOp(unary); | |
| } | |
| if (auto unary = mlir::dyn_cast<cir::UnaryOp>(op)) | |
| lowerUnaryOp(unary); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (isa<UnaryOp>(op)) | |
| if (mlir::isa<cir::UnaryOp>(op)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-canonicalize -o %t.cir %s 2>&1 | FileCheck --check-prefix=CIR-BEFORE %s | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-after=cir-lowering-prepare -o %t.cir %s 2>&1 | FileCheck --check-prefixes=CIR-AFTER %s | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll | ||
| // RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll | ||
| // RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG | ||
|
|
||
| void foo() { | ||
| int _Complex a; | ||
| int _Complex b = ~a; | ||
| } | ||
|
|
||
| // CIR-BEFORE: %[[COMPLEX:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a"] | ||
| // CIR-BEFORE: %[[RESULT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["b", init] | ||
| // CIR-BEFORE: %[[TMP:.*]] = cir.load{{.*}} %[[COMPLEX]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i> | ||
| // CIR-BEFORE: %[[COMPLEX_NOT:.*]] = cir.unary(not, %[[TMP]]) : !cir.complex<!s32i>, !cir.complex<!s32i> | ||
| // CIR-BEFORE: cir.store{{.*}} %[[COMPLEX_NOT]], %[[RESULT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>> | ||
|
|
||
| // CIR-AFTER: %[[COMPLEX:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["a"] | ||
| // CIR-AFTER: %[[RESULT:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["b", init] | ||
| // CIR-AFTER: %[[TMP:.*]] = cir.load{{.*}} %[[COMPLEX]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i> | ||
| // CIR-AFTER: %[[REAL:.*]] = cir.complex.real %[[TMP]] : !cir.complex<!s32i> -> !s32i | ||
| // CIR-AFTER: %[[IMAG:.*]] = cir.complex.imag %[[TMP]] : !cir.complex<!s32i> -> !s32i | ||
| // CIR-AFTER: %[[IMAG_MINUS:.*]] = cir.unary(minus, %[[IMAG]]) : !s32i, !s32i | ||
| // CIR-AFTER: %[[RESULT_VAL:.*]] = cir.complex.create %[[REAL]], %[[IMAG_MINUS]] : !s32i -> !cir.complex<!s32i> | ||
| // CIR-AFTER: cir.store{{.*}} %[[RESULT_VAL]], %[[RESULT]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>> | ||
|
|
||
| // LLVM: %[[COMPLEX:.*]] = alloca { i32, i32 }, i64 1, align 4 | ||
| // LLVM: %[[RESULT:.*]] = alloca { i32, i32 }, i64 1, align 4 | ||
| // LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[COMPLEX]], align 4 | ||
| // LLVM: %[[REAL:.*]] = extractvalue { i32, i32 } %[[TMP]], 0 | ||
| // LLVM: %[[IMAG:.*]] = extractvalue { i32, i32 } %[[TMP]], 1 | ||
| // LLVM: %[[IMAG_MINUS:.*]] = sub i32 0, %[[IMAG]] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test case for |
||
| // LLVM: %[[RESULT_TMP:.*]] = insertvalue { i32, i32 } {{.*}}, i32 %[[REAL]], 0 | ||
| // LLVM: %[[RESULT_VAL:.*]] = insertvalue { i32, i32 } %[[RESULT_TMP]], i32 %[[IMAG_MINUS]], 1 | ||
| // LLVM: store { i32, i32 } %[[RESULT_VAL]], ptr %[[RESULT]], align 4 | ||
|
|
||
| // OGCG: %[[COMPLEX:.*]] = alloca { i32, i32 }, align 4 | ||
| // OGCG: %[[RESULT:.*]] = alloca { i32, i32 }, align 4 | ||
| // OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX]], i32 0, i32 0 | ||
| // OGCG: %[[A_REAL:.*]] = load i32, ptr %[[A_REAL_PTR]], align 4 | ||
| // OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX]], i32 0, i32 1 | ||
| // OGCG: %[[A_IMAG:.*]] = load i32, ptr %[[A_IMAG_PTR]], align 4 | ||
| // OGCG: %[[A_IMAG_MINUS:.*]] = sub i32 0, %[[A_IMAG]] | ||
| // OGCG: %[[RESULT_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 0 | ||
| // OGCG: %[[RESULT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 1 | ||
| // OGCG: store i32 %[[A_REAL]], ptr %[[RESULT_REAL_PTR]], align 4 | ||
| // OGCG: store i32 %[[A_IMAG_MINUS]], ptr %[[RESULT_IMAG_PTR]], align 4 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.