Skip to content

Commit 1d7526b

Browse files
committed
Remove lambdas for constant creation
1 parent d45d34c commit 1d7526b

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,6 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
580580
mlir::Type llvmType = getTypeConverter()->convertType(type);
581581
mlir::Location loc = op.getLoc();
582582

583-
auto createIntConstant = [&](int64_t value) -> mlir::Value {
584-
return rewriter.create<mlir::LLVM::ConstantOp>(
585-
loc, llvmType, mlir::IntegerAttr::get(llvmType, value));
586-
};
587-
588-
auto createFloatConstant = [&](double value) -> mlir::Value {
589-
mlir::FloatAttr attr = rewriter.getFloatAttr(llvmType, value);
590-
return rewriter.create<mlir::LLVM::ConstantOp>(loc, llvmType, attr);
591-
};
592-
593583
// Integer unary operations: + - ~ ++ --
594584
if (mlir::isa<cir::IntType>(elementType)) {
595585
mlir::LLVM::IntegerOverflowFlags maybeNSW =
@@ -602,50 +592,67 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
602592
}
603593

604594
switch (op.getKind()) {
605-
case cir::UnaryOpKind::Inc:
595+
case cir::UnaryOpKind::Inc: {
606596
assert(!isVector && "++ not allowed on vector types");
597+
mlir::LLVM::ConstantOp one = rewriter.create<mlir::LLVM::ConstantOp>(
598+
loc, llvmType, mlir::IntegerAttr::get(llvmType, 1));
607599
rewriter.replaceOpWithNewOp<mlir::LLVM::AddOp>(
608-
op, llvmType, adaptor.getInput(), createIntConstant(1), maybeNSW);
600+
op, llvmType, adaptor.getInput(), one, maybeNSW);
609601
return mlir::success();
610-
case cir::UnaryOpKind::Dec:
602+
}
603+
case cir::UnaryOpKind::Dec: {
611604
assert(!isVector && "-- not allowed on vector types");
605+
mlir::LLVM::ConstantOp one = rewriter.create<mlir::LLVM::ConstantOp>(
606+
loc, llvmType, mlir::IntegerAttr::get(llvmType, 1));
612607
rewriter.replaceOpWithNewOp<mlir::LLVM::SubOp>(
613-
op, llvmType, adaptor.getInput(), createIntConstant(1), maybeNSW);
608+
op, llvmType, adaptor.getInput(), one, maybeNSW);
614609
return mlir::success();
610+
}
615611
case cir::UnaryOpKind::Plus:
616612
rewriter.replaceOp(op, adaptor.getInput());
617613
return mlir::success();
618-
case cir::UnaryOpKind::Minus:
614+
case cir::UnaryOpKind::Minus: {
619615
assert(!isVector &&
620616
"Add vector handling when vector types are supported");
617+
mlir::LLVM::ConstantOp zero = rewriter.create<mlir::LLVM::ConstantOp>(
618+
loc, llvmType, mlir::IntegerAttr::get(llvmType, 0));
621619
rewriter.replaceOpWithNewOp<mlir::LLVM::SubOp>(
622-
op, llvmType, createIntConstant(0), adaptor.getInput(), maybeNSW);
620+
op, llvmType, zero, adaptor.getInput(), maybeNSW);
623621
return mlir::success();
624-
625-
case cir::UnaryOpKind::Not:
622+
}
623+
case cir::UnaryOpKind::Not: {
626624
// bit-wise compliment operator, implemented as an XOR with -1.
627625
assert(!isVector &&
628626
"Add vector handling when vector types are supported");
627+
mlir::LLVM::ConstantOp minusOne = rewriter.create<mlir::LLVM::ConstantOp>(
628+
loc, llvmType, mlir::IntegerAttr::get(llvmType, -1));
629629
rewriter.replaceOpWithNewOp<mlir::LLVM::XOrOp>(
630-
op, llvmType, adaptor.getInput(), createIntConstant(-1));
630+
op, llvmType, adaptor.getInput(), minusOne);
631631
return mlir::success();
632632
}
633+
}
633634
llvm_unreachable("Unexpected unary op for int");
634635
}
635636

636637
// Floating point unary operations: + - ++ --
637638
if (mlir::isa<cir::CIRFPTypeInterface>(elementType)) {
638639
switch (op.getKind()) {
639-
case cir::UnaryOpKind::Inc:
640+
case cir::UnaryOpKind::Inc: {
640641
assert(!isVector && "++ not allowed on vector types");
641-
rewriter.replaceOpWithNewOp<mlir::LLVM::FAddOp>(
642-
op, llvmType, createFloatConstant(1.0), adaptor.getInput());
642+
mlir::LLVM::ConstantOp one = rewriter.create<mlir::LLVM::ConstantOp>(
643+
loc, llvmType, rewriter.getFloatAttr(llvmType, 1.0));
644+
rewriter.replaceOpWithNewOp<mlir::LLVM::FAddOp>(op, llvmType, one,
645+
adaptor.getInput());
643646
return mlir::success();
644-
case cir::UnaryOpKind::Dec:
647+
}
648+
case cir::UnaryOpKind::Dec: {
645649
assert(!isVector && "-- not allowed on vector types");
646-
rewriter.replaceOpWithNewOp<mlir::LLVM::FAddOp>(
647-
op, llvmType, createFloatConstant(-1.0), adaptor.getInput());
650+
mlir::LLVM::ConstantOp minusOne = rewriter.create<mlir::LLVM::ConstantOp>(
651+
loc, llvmType, rewriter.getFloatAttr(llvmType, -1.0));
652+
rewriter.replaceOpWithNewOp<mlir::LLVM::FAddOp>(op, llvmType, minusOne,
653+
adaptor.getInput());
648654
return mlir::success();
655+
}
649656
case cir::UnaryOpKind::Plus:
650657
rewriter.replaceOp(op, adaptor.getInput());
651658
return mlir::success();
@@ -670,12 +677,15 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
670677
// Some of these are allowed in source code, but we shouldn't get here
671678
// with a boolean type.
672679
return op.emitError() << "Unsupported unary operation on boolean type";
673-
case cir::UnaryOpKind::Not:
680+
case cir::UnaryOpKind::Not: {
674681
assert(!isVector && "NYI: op! on vector mask");
675-
rewriter.replaceOpWithNewOp<mlir::LLVM::XOrOp>(
676-
op, llvmType, adaptor.getInput(), createIntConstant(1));
682+
mlir::LLVM::ConstantOp one = rewriter.create<mlir::LLVM::ConstantOp>(
683+
loc, llvmType, rewriter.getIntegerAttr(llvmType, 1));
684+
rewriter.replaceOpWithNewOp<mlir::LLVM::XOrOp>(op, llvmType,
685+
adaptor.getInput(), one);
677686
return mlir::success();
678687
}
688+
}
679689
llvm_unreachable("Unexpected unary op for bool");
680690
}
681691

0 commit comments

Comments
 (0)