Skip to content

Commit

Permalink
[MLIR] Replace std ops with arith dialect ops
Browse files Browse the repository at this point in the history
Precursor: https://reviews.llvm.org/D110200

Removed redundant ops from the standard dialect that were moved to the
`arith` or `math` dialects.

Renamed all instances of operations in the codebase and in tests.

Reviewed By: rriddle, jpienaar

Differential Revision: https://reviews.llvm.org/D110797
  • Loading branch information
Mogball committed Oct 13, 2021
1 parent 666accf commit a54f4ea
Show file tree
Hide file tree
Showing 809 changed files with 22,191 additions and 21,534 deletions.
8 changes: 4 additions & 4 deletions flang/include/flang/Lower/CharacterRuntime.h
Expand Up @@ -18,17 +18,17 @@ class AbstractConverter;
/// Generate call to a character comparison for two ssa-values of type
/// `boxchar`.
mlir::Value genBoxCharCompare(AbstractConverter &converter, mlir::Location loc,
mlir::CmpIPredicate cmp, mlir::Value lhs,
mlir::arith::CmpIPredicate cmp, mlir::Value lhs,
mlir::Value rhs);

/// Generate call to a character comparison op for two unboxed variables. There
/// are 4 arguments, 2 for the lhs and 2 for the rhs. Each CHARACTER must pass a
/// reference to its buffer (`ref<char<K>>`) and its LEN type parameter (some
/// integral type).
mlir::Value genRawCharCompare(AbstractConverter &converter, mlir::Location loc,
mlir::CmpIPredicate cmp, mlir::Value lhsBuff,
mlir::Value lhsLen, mlir::Value rhsBuff,
mlir::Value rhsLen);
mlir::arith::CmpIPredicate cmp,
mlir::Value lhsBuff, mlir::Value lhsLen,
mlir::Value rhsBuff, mlir::Value rhsLen);

} // namespace lower
} // namespace Fortran
Expand Down
6 changes: 3 additions & 3 deletions flang/include/flang/Lower/Support/Utils.h
Expand Up @@ -30,9 +30,9 @@ inline llvm::StringRef toStringRef(const Fortran::parser::CharBlock &cb) {
}

namespace fir {
/// Return the integer value of a ConstantOp.
inline std::int64_t toInt(mlir::ConstantOp cop) {
return cop.getValue().cast<mlir::IntegerAttr>().getValue().getSExtValue();
/// Return the integer value of a arith::ConstantOp.
inline std::int64_t toInt(mlir::arith::ConstantOp cop) {
return cop.value().cast<mlir::IntegerAttr>().getValue().getSExtValue();
}
} // namespace fir

Expand Down
3 changes: 2 additions & 1 deletion flang/include/flang/Optimizer/Dialect/FIROps.h
Expand Up @@ -10,6 +10,7 @@
#define FORTRAN_OPTIMIZER_DIALECT_FIROPS_H

#include "flang/Optimizer/Dialect/FIRType.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
#include "mlir/Dialect/StandardOps/IR/Ops.h"
#include "mlir/Interfaces/LoopLikeInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
Expand All @@ -23,7 +24,7 @@ class DoLoopOp;
class RealAttr;

void buildCmpCOp(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::CmpFPredicate predicate, mlir::Value lhs,
mlir::arith::CmpFPredicate predicate, mlir::Value lhs,
mlir::Value rhs);
unsigned getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases,
unsigned dest);
Expand Down
14 changes: 7 additions & 7 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Expand Up @@ -310,7 +310,7 @@ def fir_CharConvertOp : fir_Op<"char_convert", []> {
argument. The length of the !fir.char type is ignored.

```mlir
fir.char_convert %1 for %2 to %3 : !fir.ref<!fir.char<1,?>>, i32,
fir.char_convert %1 for %2 to %3 : !fir.ref<!fir.char<1,?>>, i32,
!fir.ref<!fir.char<2,20>>
```

Expand Down Expand Up @@ -2544,7 +2544,7 @@ def fir_CmpcOp : fir_Op<"cmpc",

let printer = "printCmpcOp(p, *this);";

let builders = [OpBuilder<(ins "mlir::CmpFPredicate":$predicate,
let builders = [OpBuilder<(ins "mlir::arith::CmpFPredicate":$predicate,
"mlir::Value":$lhs, "mlir::Value":$rhs), [{
buildCmpCOp($_builder, $_state, predicate, lhs, rhs);
}]>];
Expand All @@ -2554,12 +2554,12 @@ def fir_CmpcOp : fir_Op<"cmpc",
return "predicate";
}

CmpFPredicate getPredicate() {
return (CmpFPredicate)(*this)->getAttrOfType<mlir::IntegerAttr>(
arith::CmpFPredicate getPredicate() {
return (arith::CmpFPredicate)(*this)->getAttrOfType<mlir::IntegerAttr>(
getPredicateAttrName()).getInt();
}

static CmpFPredicate getPredicateByName(llvm::StringRef name);
static arith::CmpFPredicate getPredicateByName(llvm::StringRef name);
}];
}

Expand Down Expand Up @@ -2676,9 +2676,9 @@ def fir_NoReassocOp : fir_OneResultOp<"no_reassoc",
operations with a single FMA operation.

```mlir
%98 = mulf %96, %97 : f32
%98 = arith.mulf %96, %97 : f32
%99 = fir.no_reassoc %98 : f32
%a0 = addf %99, %95 : f32
%a0 = arith.addf %99, %95 : f32
```
}];

Expand Down
4 changes: 3 additions & 1 deletion flang/include/flang/Optimizer/Support/InitFIR.h
Expand Up @@ -13,6 +13,7 @@
#ifndef FORTRAN_OPTIMIZER_SUPPORT_INITFIR_H
#define FORTRAN_OPTIMIZER_SUPPORT_INITFIR_H

#include "flang/Optimizer/CodeGen/CodeGen.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "mlir/Conversion/Passes.h"
#include "mlir/Dialect/Affine/Passes.h"
Expand All @@ -27,7 +28,8 @@ namespace fir::support {
#define FLANG_NONCODEGEN_DIALECT_LIST \
mlir::AffineDialect, FIROpsDialect, mlir::acc::OpenACCDialect, \
mlir::omp::OpenMPDialect, mlir::scf::SCFDialect, \
mlir::StandardOpsDialect, mlir::vector::VectorDialect
mlir::arith::ArithmeticDialect, mlir::StandardOpsDialect, \
mlir::vector::VectorDialect

// The definitive list of dialects used by flang.
#define FLANG_DIALECT_LIST \
Expand Down
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/Support/Utils.h
Expand Up @@ -17,9 +17,9 @@
#include "mlir/IR/BuiltinAttributes.h"

namespace fir {
/// Return the integer value of a ConstantOp.
inline std::int64_t toInt(mlir::ConstantOp cop) {
return cop.getValue().cast<mlir::IntegerAttr>().getValue().getSExtValue();
/// Return the integer value of a arith::ConstantOp.
inline std::int64_t toInt(mlir::arith::ConstantOp cop) {
return cop.value().cast<mlir::IntegerAttr>().getValue().getSExtValue();
}
} // namespace fir

Expand Down
5 changes: 3 additions & 2 deletions flang/include/flang/Optimizer/Transforms/RewritePatterns.td
Expand Up @@ -15,6 +15,7 @@
#define FORTRAN_FIR_REWRITE_PATTERNS

include "mlir/IR/OpBase.td"
include "mlir/Dialect/Arithmetic/IR/ArithmeticOps.td"
include "mlir/Dialect/StandardOps/IR/Ops.td"
include "flang/Optimizer/Dialect/FIROps.td"

Expand Down Expand Up @@ -46,12 +47,12 @@ def CombineConvertOptPattern
,(SmallerWidthPred $arg, $irm)]>;

def createConstantOp
: NativeCodeCall<"$_builder.create<mlir::ConstantOp>"
: NativeCodeCall<"$_builder.create<mlir::arith::ConstantOp>"
"($_loc, $_builder.getIndexType(), "
"rewriter.getIndexAttr($1.dyn_cast<IntegerAttr>().getInt()))">;

def ForwardConstantConvertPattern
: Pat<(fir_ConvertOp:$res (ConstantOp:$cnt $attr)),
: Pat<(fir_ConvertOp:$res (Arith_ConstantOp:$cnt $attr)),
(createConstantOp $res, $attr),
[(IndexTypePred $res)
,(IntegerTypePred $cnt)]>;
Expand Down
22 changes: 12 additions & 10 deletions flang/lib/Lower/CharacterExpr.cpp
Expand Up @@ -268,25 +268,26 @@ void Fortran::lower::CharacterExprHelper::createAssign(
// Pad if needed.
if (!compileTimeSameLength) {
auto one = builder.createIntegerConstant(loc, lhs.getLen().getType(), 1);
auto maxPadding = builder.create<mlir::SubIOp>(loc, lhs.getLen(), one);
auto maxPadding =
builder.create<mlir::arith::SubIOp>(loc, lhs.getLen(), one);
createPadding(lhs, copyCount, maxPadding);
}
}

fir::CharBoxValue Fortran::lower::CharacterExprHelper::createConcatenate(
const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs) {
mlir::Value len =
builder.create<mlir::AddIOp>(loc, lhs.getLen(), rhs.getLen());
builder.create<mlir::arith::AddIOp>(loc, lhs.getLen(), rhs.getLen());
auto temp = createTemp(getCharacterType(rhs), len);
createCopy(temp, lhs, lhs.getLen());
auto one = builder.createIntegerConstant(loc, len.getType(), 1);
auto upperBound = builder.create<mlir::SubIOp>(loc, len, one);
auto upperBound = builder.create<mlir::arith::SubIOp>(loc, len, one);
auto lhsLen =
builder.createConvert(loc, builder.getIndexType(), lhs.getLen());
Fortran::lower::DoLoopHelper{builder, loc}.createLoop(
lhs.getLen(), upperBound, one,
[&](Fortran::lower::FirOpBuilder &bldr, mlir::Value index) {
auto rhsIndex = bldr.create<mlir::SubIOp>(loc, index, lhsLen);
auto rhsIndex = bldr.create<mlir::arith::SubIOp>(loc, index, lhsLen);
auto charVal = createLoadCharAt(rhs, rhsIndex);
createStoreCharAt(temp, index, charVal);
});
Expand All @@ -312,7 +313,8 @@ fir::CharBoxValue Fortran::lower::CharacterExprHelper::createSubstring(
auto lowerBound = castBounds[0];
// FIR CoordinateOp is zero based but Fortran substring are one based.
auto one = builder.createIntegerConstant(loc, lowerBound.getType(), 1);
auto offset = builder.create<mlir::SubIOp>(loc, lowerBound, one).getResult();
auto offset =
builder.create<mlir::arith::SubIOp>(loc, lowerBound, one).getResult();
auto idxType = builder.getIndexType();
if (offset.getType() != idxType)
offset = builder.createConvert(loc, idxType, offset);
Expand All @@ -323,17 +325,17 @@ fir::CharBoxValue Fortran::lower::CharacterExprHelper::createSubstring(
mlir::Value substringLen{};
if (nbounds < 2) {
substringLen =
builder.create<mlir::SubIOp>(loc, str.getLen(), castBounds[0]);
builder.create<mlir::arith::SubIOp>(loc, str.getLen(), castBounds[0]);
} else {
substringLen =
builder.create<mlir::SubIOp>(loc, castBounds[1], castBounds[0]);
builder.create<mlir::arith::SubIOp>(loc, castBounds[1], castBounds[0]);
}
substringLen = builder.create<mlir::AddIOp>(loc, substringLen, one);
substringLen = builder.create<mlir::arith::AddIOp>(loc, substringLen, one);

// Set length to zero if bounds were reversed (Fortran 2018 9.4.1)
auto zero = builder.createIntegerConstant(loc, substringLen.getType(), 0);
auto cdt = builder.create<mlir::CmpIOp>(loc, mlir::CmpIPredicate::slt,
substringLen, zero);
auto cdt = builder.create<mlir::arith::CmpIOp>(
loc, mlir::arith::CmpIPredicate::slt, substringLen, zero);
substringLen = builder.create<mlir::SelectOp>(loc, cdt, zero, substringLen);

return {substringRef, substringLen};
Expand Down
18 changes: 8 additions & 10 deletions flang/lib/Lower/CharacterRuntime.cpp
Expand Up @@ -85,11 +85,10 @@ static int discoverKind(mlir::Type ty) {
// Lower character operations
//===----------------------------------------------------------------------===//

mlir::Value
Fortran::lower::genRawCharCompare(Fortran::lower::AbstractConverter &converter,
mlir::Location loc, mlir::CmpIPredicate cmp,
mlir::Value lhsBuff, mlir::Value lhsLen,
mlir::Value rhsBuff, mlir::Value rhsLen) {
mlir::Value Fortran::lower::genRawCharCompare(
Fortran::lower::AbstractConverter &converter, mlir::Location loc,
mlir::arith::CmpIPredicate cmp, mlir::Value lhsBuff, mlir::Value lhsLen,
mlir::Value rhsBuff, mlir::Value rhsLen) {
auto &builder = converter.getFirOpBuilder();
mlir::FuncOp beginFunc;
switch (discoverKind(lhsBuff.getType())) {
Expand All @@ -113,13 +112,12 @@ Fortran::lower::genRawCharCompare(Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<mlir::Value, 4> args = {lptr, rptr, llen, rlen};
auto tri = builder.create<mlir::CallOp>(loc, beginFunc, args).getResult(0);
auto zero = builder.createIntegerConstant(loc, tri.getType(), 0);
return builder.create<mlir::CmpIOp>(loc, cmp, tri, zero);
return builder.create<mlir::arith::CmpIOp>(loc, cmp, tri, zero);
}

mlir::Value
Fortran::lower::genBoxCharCompare(Fortran::lower::AbstractConverter &converter,
mlir::Location loc, mlir::CmpIPredicate cmp,
mlir::Value lhs, mlir::Value rhs) {
mlir::Value Fortran::lower::genBoxCharCompare(
Fortran::lower::AbstractConverter &converter, mlir::Location loc,
mlir::arith::CmpIPredicate cmp, mlir::Value lhs, mlir::Value rhs) {
auto &builder = converter.getFirOpBuilder();
Fortran::lower::CharacterExprHelper helper{builder, loc};
auto lhsPair = helper.materializeCharacter(lhs);
Expand Down
14 changes: 8 additions & 6 deletions flang/lib/Lower/ComplexExpr.cpp
Expand Up @@ -46,13 +46,15 @@ mlir::Value Fortran::lower::ComplexExprHelper::createComplexCompare(
auto imag1 = extract<Part::Imag>(cplx1);
auto imag2 = extract<Part::Imag>(cplx2);

mlir::CmpFPredicate predicate =
eq ? mlir::CmpFPredicate::UEQ : mlir::CmpFPredicate::UNE;
mlir::arith::CmpFPredicate predicate =
eq ? mlir::arith::CmpFPredicate::UEQ : mlir::arith::CmpFPredicate::UNE;
mlir::Value realCmp =
builder.create<mlir::CmpFOp>(loc, predicate, real1, real2);
builder.create<mlir::arith::CmpFOp>(loc, predicate, real1, real2);
mlir::Value imagCmp =
builder.create<mlir::CmpFOp>(loc, predicate, imag1, imag2);
builder.create<mlir::arith::CmpFOp>(loc, predicate, imag1, imag2);

return eq ? builder.create<mlir::AndOp>(loc, realCmp, imagCmp).getResult()
: builder.create<mlir::OrOp>(loc, realCmp, imagCmp).getResult();
return eq ? builder.create<mlir::arith::AndIOp>(loc, realCmp, imagCmp)
.getResult()
: builder.create<mlir::arith::OrIOp>(loc, realCmp, imagCmp)
.getResult();
}
2 changes: 1 addition & 1 deletion flang/lib/Lower/DoLoopHelper.cpp
Expand Up @@ -39,6 +39,6 @@ void Fortran::lower::DoLoopHelper::createLoop(
auto indexType = builder.getIndexType();
auto zero = builder.createIntegerConstant(loc, indexType, 0);
auto one = builder.createIntegerConstant(loc, count.getType(), 1);
auto up = builder.create<mlir::SubIOp>(loc, count, one);
auto up = builder.create<mlir::arith::SubIOp>(loc, count, one);
createLoop(zero, up, one, bodyGenerator);
}
7 changes: 4 additions & 3 deletions flang/lib/Lower/FIRBuilder.cpp
Expand Up @@ -48,12 +48,13 @@ Fortran::lower::FirOpBuilder::createNullConstant(mlir::Location loc) {

mlir::Value Fortran::lower::FirOpBuilder::createIntegerConstant(
mlir::Location loc, mlir::Type ty, std::int64_t cst) {
return create<mlir::ConstantOp>(loc, ty, getIntegerAttr(ty, cst));
return create<mlir::arith::ConstantOp>(loc, ty, getIntegerAttr(ty, cst));
}

mlir::Value Fortran::lower::FirOpBuilder::createRealConstant(
mlir::Location loc, mlir::Type realType, const llvm::APFloat &val) {
return create<mlir::ConstantOp>(loc, realType, getFloatAttr(realType, val));
return create<mlir::arith::ConstantOp>(loc, realType,
getFloatAttr(realType, val));
}

mlir::Value
Expand All @@ -67,7 +68,7 @@ Fortran::lower::FirOpBuilder::createRealZeroConstant(mlir::Location loc,
} else { // mlir::FloatType.
attr = getZeroAttr(realType);
}
return create<mlir::ConstantOp>(loc, realType, attr);
return create<mlir::arith::ConstantOp>(loc, realType, attr);
}

mlir::Value Fortran::lower::FirOpBuilder::allocateLocal(
Expand Down

0 comments on commit a54f4ea

Please sign in to comment.