[clang] [CIR] Upstream SelectOp and ShiftOp (PR #133405)
Bruno Cardoso Lopes via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 28 15:36:28 PDT 2025
================
@@ -1117,6 +1117,91 @@ mlir::LogicalResult CIRToLLVMBinOpLowering::matchAndRewrite(
return mlir::LogicalResult::success();
}
+mlir::LogicalResult CIRToLLVMShiftOpLowering::matchAndRewrite(
+ cir::ShiftOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ auto cirAmtTy = mlir::dyn_cast<cir::IntType>(op.getAmount().getType());
+ auto cirValTy = mlir::dyn_cast<cir::IntType>(op.getValue().getType());
+
+ // Operands could also be vector type
+ assert(!cir::MissingFeatures::vectorType());
+ mlir::Type llvmTy = getTypeConverter()->convertType(op.getType());
+ mlir::Value amt = adaptor.getAmount();
+ mlir::Value val = adaptor.getValue();
+
+ // TODO(cir): Assert for vector types
+ assert((cirValTy && cirAmtTy) &&
+ "shift input type must be integer or vector type, otherwise NYI");
+
+ assert((cirValTy == op.getType()) && "inconsistent operands' types NYI");
+
+ // Ensure shift amount is the same type as the value. Some undefined
+ // behavior might occur in the casts below as per [C99 6.5.7.3].
+ // Vector type shift amount needs no cast as type consistency is expected to
+ // be already be enforced at CIRGen.
+ if (cirAmtTy)
+ amt = getLLVMIntCast(rewriter, amt, mlir::cast<mlir::IntegerType>(llvmTy),
+ !cirAmtTy.isSigned(), cirAmtTy.getWidth(),
+ cirValTy.getWidth());
+
+ // Lower to the proper LLVM shift operation.
+ if (op.getIsShiftleft()) {
+ rewriter.replaceOpWithNewOp<mlir::LLVM::ShlOp>(op, llvmTy, val, amt);
+ } else {
+ assert(!cir::MissingFeatures::vectorType());
+ bool isUnsigned = !cirValTy.isSigned();
+ if (isUnsigned)
+ rewriter.replaceOpWithNewOp<mlir::LLVM::LShrOp>(op, llvmTy, val, amt);
+ else
+ rewriter.replaceOpWithNewOp<mlir::LLVM::AShrOp>(op, llvmTy, val, amt);
+ }
+
+ return mlir::success();
+}
+
+mlir::LogicalResult CIRToLLVMSelectOpLowering::matchAndRewrite(
+ cir::SelectOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ auto getConstantBool = [](mlir::Value value) -> std::optional<bool> {
----------------
bcardosolopes wrote:
Let's rely on MLIR's usage of value semantics here.
This lambda should just return an `cir::BoolAttr`, so you can write:
```
cir::BoolAttr trueValue = getConstantBool(op.getTrueValue());
...
if (trueValue && !trueValue.getValue()) {
}
```
The lambda should return `{}`/`nullptr` in the case attribute not found.
https://github.com/llvm/llvm-project/pull/133405
More information about the cfe-commits
mailing list