@@ -580,16 +580,6 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
580
580
mlir::Type llvmType = getTypeConverter ()->convertType (type);
581
581
mlir::Location loc = op.getLoc ();
582
582
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
-
593
583
// Integer unary operations: + - ~ ++ --
594
584
if (mlir::isa<cir::IntType>(elementType)) {
595
585
mlir::LLVM::IntegerOverflowFlags maybeNSW =
@@ -602,50 +592,67 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
602
592
}
603
593
604
594
switch (op.getKind ()) {
605
- case cir::UnaryOpKind::Inc:
595
+ case cir::UnaryOpKind::Inc: {
606
596
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 ));
607
599
rewriter.replaceOpWithNewOp <mlir::LLVM::AddOp>(
608
- op, llvmType, adaptor.getInput (), createIntConstant ( 1 ) , maybeNSW);
600
+ op, llvmType, adaptor.getInput (), one , maybeNSW);
609
601
return mlir::success ();
610
- case cir::UnaryOpKind::Dec:
602
+ }
603
+ case cir::UnaryOpKind::Dec: {
611
604
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 ));
612
607
rewriter.replaceOpWithNewOp <mlir::LLVM::SubOp>(
613
- op, llvmType, adaptor.getInput (), createIntConstant ( 1 ) , maybeNSW);
608
+ op, llvmType, adaptor.getInput (), one , maybeNSW);
614
609
return mlir::success ();
610
+ }
615
611
case cir::UnaryOpKind::Plus:
616
612
rewriter.replaceOp (op, adaptor.getInput ());
617
613
return mlir::success ();
618
- case cir::UnaryOpKind::Minus:
614
+ case cir::UnaryOpKind::Minus: {
619
615
assert (!isVector &&
620
616
" 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 ));
621
619
rewriter.replaceOpWithNewOp <mlir::LLVM::SubOp>(
622
- op, llvmType, createIntConstant ( 0 ) , adaptor.getInput (), maybeNSW);
620
+ op, llvmType, zero , adaptor.getInput (), maybeNSW);
623
621
return mlir::success ();
624
-
625
- case cir::UnaryOpKind::Not:
622
+ }
623
+ case cir::UnaryOpKind::Not: {
626
624
// bit-wise compliment operator, implemented as an XOR with -1.
627
625
assert (!isVector &&
628
626
" 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 ));
629
629
rewriter.replaceOpWithNewOp <mlir::LLVM::XOrOp>(
630
- op, llvmType, adaptor.getInput (), createIntConstant (- 1 ) );
630
+ op, llvmType, adaptor.getInput (), minusOne );
631
631
return mlir::success ();
632
632
}
633
+ }
633
634
llvm_unreachable (" Unexpected unary op for int" );
634
635
}
635
636
636
637
// Floating point unary operations: + - ++ --
637
638
if (mlir::isa<cir::CIRFPTypeInterface>(elementType)) {
638
639
switch (op.getKind ()) {
639
- case cir::UnaryOpKind::Inc:
640
+ case cir::UnaryOpKind::Inc: {
640
641
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 ());
643
646
return mlir::success ();
644
- case cir::UnaryOpKind::Dec:
647
+ }
648
+ case cir::UnaryOpKind::Dec: {
645
649
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 ());
648
654
return mlir::success ();
655
+ }
649
656
case cir::UnaryOpKind::Plus:
650
657
rewriter.replaceOp (op, adaptor.getInput ());
651
658
return mlir::success ();
@@ -670,12 +677,15 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
670
677
// Some of these are allowed in source code, but we shouldn't get here
671
678
// with a boolean type.
672
679
return op.emitError () << " Unsupported unary operation on boolean type" ;
673
- case cir::UnaryOpKind::Not:
680
+ case cir::UnaryOpKind::Not: {
674
681
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);
677
686
return mlir::success ();
678
687
}
688
+ }
679
689
llvm_unreachable (" Unexpected unary op for bool" );
680
690
}
681
691
0 commit comments