[PSA] Removed Arithmetic ops from Standard

https://reviews.llvm.org/D110797 is about to land/just landed, which moved the following ops from the Standard Dialect:

MLIR asm C++
addiarith.addi AddIOparith::AddIOp
subiarith.subi SubIOparith::SubIOp
muliarith.muli MulIOparith::MulIOp
divi_unsignedarith.divui UnsignedDivIOparith::DivUIOp
divi_signedarith.divsi SignedDivIOparith::DivSIOp
ceildivi_signedarith.ceildivsi SignedCeilDivIOparith::CeilDivSIOp
floordivi_signedarith.floordivsi SignedFloorDivIOparith::FloorDivSIOp
remi_unsignedarith.remui UnsignedRemIOparith::RemUIOp
remi_signedarith.remsi SignedRemIOparith::RemSIOp
andarith.andi AndOparith::AndIOp
orarith.ori OrOparith::OrIOp
xorarith.xori XOrOparith::XOrIOp
shift_leftarith.shli ShiftLeftOparith::ShLIOp
shift_right_unsignedarith.shrui UnsignedShiftRightOparith::ShRUIOp
shift_right_signedarith.shrsi SignedShiftRightOparith::ShRSIOp
negfarith.negf NegFOparith::NegFOp
addfarith.addf AddFOparith::AddFOp
subfarith.subf SubFOparith::SubFOp
mulfarith.mulf MulFOparith::MulFOp
divfarith.divf DivFOparith::DivFOp
remfarith.remf RemFOparith::RemFOp
zextiarith.extui ZeroExtendIOparith::ExtUIOp
sextiarith.extsi SignExtendIOparith::ExtSIOp
fpextarith.extf FPExtOparith::ExtFOp
trunciarith.trunci TruncateIOparith::TruncIOp
fptruncarith.truncf FPTruncOparith::TruncFOp
uitofparith.uitofp UIToFPOparith::UIToFPOp
sitofparith.sitofp SIToFPOparith::SIToFPOp
fptouiarith.fptoui FPToUIOparith::FPToUIOp
fptosiarith.fptosi FPToSIOparith::FPToSIOp
index_castarith.index_cast IndexCastOparith::IndexCastOp
bitcastarith.bitcast BitcastOparith::BitcastOp
cmpiarith.cmpi CmpIOparith::CmpIOp
cmpfarith.cmpf CmpFOparith::CmpFOp
absfmath.abs AbsFOpmath::AbsOp
ceilfmath.ceil CeilFOpmath::CeilOp
floorfmath.floor FloorFOpmath::FloorOp
copysignmath.copysign CopySignOpmath::CopySignOp
fmafmath.fma FmaFOpmath::FmaOp

ConstantOp remains in Standard. However, it can no longer be used to create integers, floats, vectors, or tensors. These are now created using arith::ConstantOp or arith.constant. std.constant will continue to be used to create complex constants, function references, etc. ConstantIntOp, ConstantFloatOp, and ConstantIndexOp are also under arith:: namespace.

Dependencies

Any users of MLIRStandardTo* will also need to depend on MLIRArithmeticTo* and call populateArithmeticTo* to include the moved patterns. Users of StandardOpsDialect will most likely need to depend on ArithmeticDialect if they use the moved ops.

Script

I used the following script to help with renaming:

declare -A ops_cpp=(                                                                                                                                                                                  
  [arith::ConstantIndexOp]=ConstantIndexOp                                                                                                                                                          
  [arith::ConstantIntOp]=ConstantIntOp                                                                                                                                                              
  [arith::ConstantFloatOp]=ConstantFloatOp                                                                                                                                                          
  [arith::AddIOp]=AddIOp                                                                                                                                                                            
  [arith::SubIOp]=SubIOp                                                                                                                                                                            
  [arith::MulIOp]=MulIOp                                                                                                                                                                            
  [arith::DivUIOp]=UnsignedDivIOp                                                                                                                                                                   
  [arith::DivSIOp]=SignedDivIOp                                                                                                                                                                     
  [arith::CeilDivSIOp]=SignedCeilDivIOp                                                                                                                                                             
  [arith::FloorDivSIOp]=SignedFloorDivIOp                                                                                                                                                           
  [arith::RemUIOp]=UnsignedRemIOp                                                                                                                                                                   
  [arith::RemSIOp]=SignedRemIOp                                                                                                                                                                     
  [arith::AndIOp]=AndOp                                                                                                                                                                             
  [arith::OrIOp]=OrOp                                                                                                                                                                               
  [arith::XOrIOp]=XOrOp                                                                                                                                                                             
  [arith::ShLIOp]=ShiftLeftOp                                                                                                                                                                       
  [arith::ShRUIOp]=UnsignedShiftRightOp                                                                                                                                                             
  [arith::ShRSIOp]=SignedShiftRightOp                                                                                                                                                               
  [arith::NegFOp]=NegFOp                                                                                                                                                                            
  [arith::AddFOp]=AddFOp                                                                                                                                                                            
  [arith::SubFOp]=SubFOp                                                                                                                                                                            
  [arith::MulFOp]=MulFOp                                                                                                                                                                            
  [arith::DivFOp]=DivFOp                                                                                                                                                                            
  [arith::RemFOp]=RemFOp                                                                                                                                                                            
  [arith::ExtUIOp]=ZeroExtendIOp                                                                                                                                                                    
  [arith::ExtSIOp]=SignExtendIOp                                                                                                                                                                    
  [arith::ExtFOp]=FPExtOp                                                                                                                                                                           
  [arith::TruncIOp]=TruncateIOp                                                                                                                                                                     
  [arith::TruncFOp]=FPTruncOp                                                                                                                                                                       
  [arith::UIToFPOp]=UIToFPOp                                                                                                                                                                        
  [arith::SIToFPOp]=SIToFPOp                                                                                                                                                                        
  [arith::FPToUIOp]=FPToUIOp                                                                                                                                                                        
  [arith::FPToSIOp]=FPToSIOp                                                                                                                                                                        
  [arith::IndexCastOp]=IndexCastOp                                                                                                                                                                  
  [arith::BitcastOp]=BitcastOp                                                                                                                                                                      
  [arith::CmpIOp]=CmpIOp                                                                                                                                                                            
  [arith::CmpFOp]=CmpFOp                                                                                                                                                                            
  [math::AbsOp]=AbsFOp                                                                                                                                                                              
  [math::CeilOp]=CeilFOp                                                                                                                                                                            
  [math::FloorOp]=FloorFOp                                                                                                                                                                          
  [math::CopySignOp]=CopySignOp
  [math::FmaOp]=FmaFOp
  [arith::CmpIPredicate]=CmpIPredicate
  [arith::CmpFPredicate]=CmpFPredicate
)
declare -A ops_mlir=(                                                                                                                                                                             
  [arith.addi]=addi                                                                                                                                                                                 
  [arith.subi]=subi                                                                                                                                                                                 
  [arith.muli]=muli                                                                                                                                                                                 
  [arith.divui]=divi_unsigned                                                                                                                                                                       
  [arith.divsi]=divi_signed
  [arith.ceildivsi]=ceildivi_signed
  [arith.floordivsi]=floordivi_signed
  [arith.remui]=remi_unsigned
  [arith.remsi]=remi_signed
  [arith.andi]=and
  [arith.ori]=or
  [arith.xori]=xor
  [arith.shli]=shift_left
  [arith.shrui]=shift_right_unsigned
  [arith.shrsi]=shift_right_signed
  [arith.negf]=negf
  [arith.addf]=addf
  [arith.subf]=subf
  [arith.mulf]=mulf
  [arith.divf]=divf
  [arith.remf]=remf
  [arith.extui]=zexti
  [arith.extsi]=sexti
  [arith.extf]=fpext
  [arith.trunci]=trunci
  [arith.truncf]=fptrunc
  [arith.uitofp]=uitofp
  [arith.sitofp]=sitofp
  [arith.fptoui]=fptoui
  [arith.fptosi]=fptosi
  [arith.index_cast]=index_cast
  [arith.bitcast]=bitcast
  [arith.cmpi]=cmpi
  [arith.cmpf]=cmpf
  [math.abs]=absf
  [math.ceil]=ceilf
  [math.floor]=floorf
  [math.copysign]=copysign
  [math.fma]=fmaf
)

replace_all_cpp() {
  for i in "${!ops_cpp@]}"; do
      j=${ops_cpp[$i]}
      lookup="(?<!::)\b$j\b"
      grep -P "$lookup" -r $1 | xargs -n 4 -P 4 perl -i -pe "s/$lookup/$i/g"
      lookup="(?<!::)\b$jAdaptor\b"
      grep -P "$lookup" -r $1 | xargs -n 4 -P 4 perl -i -pe "s/$lookup/$iAdaptor/g"
      lookup="mlir::\b$j\b"
      grep -P "$lookup" -r $1 | xargs -n 4 -P 4 perl -i -pe "s/$lookup/mlir::$i/g"
  done
}

replace_all_mlir() {
  for i in "${!ops_mlir[@]}"; do
      j=${ops_mlir[$i]}
      lookup=" = \b$j\b "
      grep -P "$lookup" -r $1 | xargs -n 4 -P 4 perl -i -pe "s/$lookup/ = $i /g"
      lookup=" = \b$j\b$"
      grep -P "$lookup" -r $1 | xargs -n 4 -P 4 perl -i -pe "s/$lookup/ = $i/g"
  done
  grep -P "$lookup" -r $1 | xargs -n 4 -P 4 perl -i -pe 's/ = constant (?!\[\"u@)/ = arith.constant /g'
}

3 Likes

I’m very excited to see this, thank you for driving it forward @Mogball . The new names are great,

-Chris

1 Like

Just noticed that there are a bunch of standard op comments which still refer to arithmetic constants under std. I don’t have time to scrub them now, so just mentioning.

Good catch. Let me take a look.

Oh FYI there’s an error in the script (forgot -l on grep)

replace_all_cpp() {
  for i in "${!ops_cpp@]}"; do
      j=${ops_cpp[$i]}
      lookup="(?<!::)\b$j\b"
      grep -P "$lookup" -r $1 -l | xargs -n 4 -P 4 perl -i -pe "s/$lookup/$i/g"
      lookup="(?<!::)\b$jAdaptor\b"
      grep -P "$lookup" -r $1 -l | xargs -n 4 -P 4 perl -i -pe "s/$lookup/$iAdaptor/g"
      lookup="mlir::\b$j\b"
      grep -P "$lookup" -r $1 -l | xargs -n 4 -P 4 perl -i -pe "s/$lookup/mlir::$i/g"
  done
}

replace_all_mlir() {
  for i in "${!ops_mlir[@]}"; do
      j=${ops_mlir[$i]}
      lookup=" = \b$j\b "
      grep -P "$lookup" -r $1 -l | xargs -n 4 -P 4 perl -i -pe "s/$lookup/ = $i /g"
      lookup=" = \b$j\b$"
      grep -P "$lookup" -r $1 -l | xargs -n 4 -P 4 perl -i -pe "s/$lookup/ = $i/g"
  done
  grep -P ' = constant (?![\[\"u@])' -r $1 -l | xargs -n 4 -P 4 perl -i -pe 's/ = constant (?![\[\"u@])/ = arith.constant /g'
}

A little bit late but the docs in mlir/docs/Tutorials/Toy/Ch-6.md refers to the arith->llvm lowering pattern populate function as mlir::populateArithmeticToLLVMConversionPatterns while it’s actually mlir::arith::populateArithmeticToLLVMConversionPatterns

1 Like

Good catch. Thanks! Fixed now.