Skip to content

Commit

Permalink
[flang] Remove 'using namespace mlir;` from header files
Browse files Browse the repository at this point in the history
Currently, CGOps.h and FIROps.h contain `using namespace mlir;`. Every
file that includes one of these header files (directly and transitively)
will have the MLIR namespace enabled. With name-clashes within
sub-projects (LLVM and MLIR, MLIR and Flang), this is not desired. Also,
it is not possible to "un-use" a namespace once it is "used". Instead,
we should try to limit `using namespace` to implementation files (i.e.
*.cpp).

This patch removes `using namespace mlir;` from header files and adjusts
other files accordingly. In header and TableGen files, extra namespace
qualifier is added when referring to symbols defined in MLIR. Similar
approach is adopted in source files that didn't require many changes. In
files that would require a lot of changes, `using namespace mlir;` is
added instead.

Differential Revision: https://reviews.llvm.org/D120897
  • Loading branch information
banach-space committed Mar 9, 2022
1 parent 3d9386a commit 092601d
Show file tree
Hide file tree
Showing 31 changed files with 80 additions and 52 deletions.
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/Builder/Factory.h
Expand Up @@ -193,12 +193,12 @@ originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
dimension == mlir::cast<fir::ShapeOp>(shapeVal.getDefiningOp())
.getType()
.getRank());
auto one = builder.template create<arith::ConstantIndexOp>(loc, 1);
auto one = builder.template create<mlir::arith::ConstantIndexOp>(loc, 1);
for (auto i : llvm::enumerate(indices)) {
if (i.index() < dimension) {
assert(fir::isa_integer(i.value().getType()));
result.push_back(
builder.template create<arith::AddIOp>(loc, i.value(), one));
builder.template create<mlir::arith::AddIOp>(loc, i.value(), one));
} else {
result.push_back(i.value());
}
Expand All @@ -209,7 +209,7 @@ originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
unsigned origOff = 0;
for (auto i : llvm::enumerate(indices)) {
if (i.index() < dimension)
result.push_back(builder.template create<arith::AddIOp>(
result.push_back(builder.template create<mlir::arith::AddIOp>(
loc, i.value(), origins[origOff++]));
else
result.push_back(i.value());
Expand Down
Expand Up @@ -48,7 +48,7 @@ def CombineConvertOptPattern
def createConstantOp
: NativeCodeCall<"$_builder.create<mlir::arith::ConstantOp>"
"($_loc, $_builder.getIndexType(), "
"rewriter.getIndexAttr($1.dyn_cast<IntegerAttr>()"
"rewriter.getIndexAttr($1.dyn_cast<mlir::IntegerAttr>()"
".getInt()))">;

def ForwardConstantConvertPattern
Expand Down
2 changes: 0 additions & 2 deletions flang/include/flang/Optimizer/Dialect/FIROps.h
Expand Up @@ -15,8 +15,6 @@
#include "mlir/Interfaces/LoopLikeInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"

using namespace mlir;

namespace fir {

class FirEndOp;
Expand Down
54 changes: 27 additions & 27 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Expand Up @@ -2064,7 +2064,7 @@ def fir_LenParamIndexOp : fir_OneResultOp<"len_param_index", [NoSideEffect]> {
"mlir::Type":$recTy),
[{
$_state.addAttribute(fieldAttrName(), $_builder.getStringAttr(fieldName));
$_state.addAttribute(typeAttrName(), TypeAttr::get(recTy));
$_state.addAttribute(typeAttrName(), mlir::TypeAttr::get(recTy));
}]
>];

Expand Down Expand Up @@ -2154,7 +2154,7 @@ def fir_DoLoopOp : region_Op<"do_loop",
let extraClassDeclaration = [{
mlir::Value getInductionVar() { return getBody()->getArgument(0); }
mlir::OpBuilder getBodyBuilder() {
return OpBuilder(getBody(), std::prev(getBody()->end()));
return mlir::OpBuilder(getBody(), std::prev(getBody()->end()));
}
mlir::Block::BlockArgListType getRegionIterArgs() {
return getBody()->getArguments().drop_front();
Expand All @@ -2163,9 +2163,9 @@ def fir_DoLoopOp : region_Op<"do_loop",
return getOperands().drop_front(getNumControlOperands());
}

void setLowerBound(Value bound) { (*this)->setOperand(0, bound); }
void setUpperBound(Value bound) { (*this)->setOperand(1, bound); }
void setStep(Value step) { (*this)->setOperand(2, step); }
void setLowerBound(mlir::Value bound) { (*this)->setOperand(0, bound); }
void setUpperBound(mlir::Value bound) { (*this)->setOperand(1, bound); }
void setStep(mlir::Value step) { (*this)->setOperand(2, step); }

/// Number of region arguments for loop-carried values
unsigned getNumRegionIterArgs() {
Expand Down Expand Up @@ -2312,8 +2312,8 @@ def fir_IterWhileOp : region_Op<"iterate_while",
return getOperands().drop_front(getNumControlOperands());
}

void setLowerBound(Value bound) { (*this)->setOperand(0, bound); }
void setUpperBound(Value bound) { (*this)->setOperand(1, bound); }
void setLowerBound(mlir::Value bound) { (*this)->setOperand(0, bound); }
void setUpperBound(mlir::Value bound) { (*this)->setOperand(1, bound); }
void setStep(mlir::Value step) { (*this)->setOperand(2, step); }

/// Number of region arguments for loop-carried values
Expand Down Expand Up @@ -2376,18 +2376,18 @@ def fir_CallOp : fir_Op<"call", [CallOpInterface]> {
CArg<"mlir::ValueRange", "{}">:$operands),
[{
build($_builder, $_state,
SymbolRefAttr::get($_builder.getContext(), callee), results,
mlir::SymbolRefAttr::get($_builder.getContext(), callee), results,
operands);
}]>];

let extraClassDeclaration = [{
static constexpr StringRef getCalleeAttrNameStr() { return "callee"; }
static constexpr llvm::StringRef getCalleeAttrNameStr() { return "callee"; }

mlir::FunctionType getFunctionType();

/// Get the argument operands to the called function.
operand_range getArgOperands() {
if ((*this)->getAttrOfType<SymbolRefAttr>(getCalleeAttrName()))
if ((*this)->getAttrOfType<mlir::SymbolRefAttr>(getCalleeAttrName()))
return {arg_operand_begin(), arg_operand_end()};
return {arg_operand_begin() + 1, arg_operand_end()};
}
Expand All @@ -2396,9 +2396,9 @@ def fir_CallOp : fir_Op<"call", [CallOpInterface]> {
operand_iterator arg_operand_end() { return operand_end(); }

/// Return the callee of this operation.
CallInterfaceCallable getCallableForCallee() {
mlir::CallInterfaceCallable getCallableForCallee() {
if (auto calling =
(*this)->getAttrOfType<SymbolRefAttr>(getCalleeAttrName()))
(*this)->getAttrOfType<mlir::SymbolRefAttr>(getCalleeAttrName()))
return calling;
return getOperand(0);
}
Expand Down Expand Up @@ -2577,12 +2577,12 @@ def fir_CmpcOp : fir_Op<"cmpc",
return "predicate";
}

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

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

Expand Down Expand Up @@ -2642,16 +2642,16 @@ def fir_ConvertOp : fir_OneResultOp<"convert", [NoSideEffect]> {
let hasCanonicalizer = 1;
}

def FortranTypeAttr : Attr<And<[CPred<"$_self.isa<TypeAttr>()">,
Or<[CPred<"$_self.cast<TypeAttr>().getValue().isa<fir::CharacterType>()">,
CPred<"$_self.cast<TypeAttr>().getValue().isa<fir::ComplexType>()">,
CPred<"$_self.cast<TypeAttr>().getValue().isa<fir::IntegerType>()">,
CPred<"$_self.cast<TypeAttr>().getValue().isa<fir::LogicalType>()">,
CPred<"$_self.cast<TypeAttr>().getValue().isa<fir::RealType>()">,
CPred<"$_self.cast<TypeAttr>().getValue().isa<fir::RecordType>()">]>]>,
def FortranTypeAttr : Attr<And<[CPred<"$_self.isa<mlir::TypeAttr>()">,
Or<[CPred<"$_self.cast<mlir::TypeAttr>().getValue().isa<fir::CharacterType>()">,
CPred<"$_self.cast<mlir::TypeAttr>().getValue().isa<fir::ComplexType>()">,
CPred<"$_self.cast<mlir::TypeAttr>().getValue().isa<fir::IntegerType>()">,
CPred<"$_self.cast<mlir::TypeAttr>().getValue().isa<fir::LogicalType>()">,
CPred<"$_self.cast<mlir::TypeAttr>().getValue().isa<fir::RealType>()">,
CPred<"$_self.cast<mlir::TypeAttr>().getValue().isa<fir::RecordType>()">]>]>,
"Fortran surface type"> {
let storageType = [{ TypeAttr }];
let returnType = "Type";
let storageType = [{ ::mlir::TypeAttr }];
let returnType = "mlir::Type";
let convertFromStorage = "$_self.getValue().cast<Type>()";
}

Expand Down Expand Up @@ -2773,7 +2773,7 @@ def fir_GlobalOp : fir_Op<"global", [IsolatedFromAbove, Symbol]> {
mlir::Type resultType();

/// Return the initializer attribute if it exists, or a null attribute.
Attribute getValueOrNull() { return getInitVal().getValueOr(Attribute()); }
mlir::Attribute getValueOrNull() { return getInitVal().getValueOr(mlir::Attribute()); }

/// Append the next initializer value to the `GlobalOp` to construct
/// the variable's initial value.
Expand All @@ -2783,11 +2783,11 @@ def fir_GlobalOp : fir_Op<"global", [IsolatedFromAbove, Symbol]> {
mlir::Block &getBlock() { return getRegion().front(); }

/// Determine if `linkage` is a supported keyword
static mlir::ParseResult verifyValidLinkage(StringRef linkage);
static mlir::ParseResult verifyValidLinkage(llvm::StringRef linkage);

bool hasInitializationBody() {
return ((*this)->getNumRegions() == 1) && !getRegion().empty() &&
!isa<fir::FirEndOp>(getBlock().front());
!mlir::isa<fir::FirEndOp>(getBlock().front());
}

mlir::FlatSymbolRefAttr getSymbol() {
Expand Down
7 changes: 4 additions & 3 deletions flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
Expand Up @@ -17,15 +17,16 @@ namespace fir {

/// return true iff the Operation is a non-volatile LoadOp
inline bool nonVolatileLoad(mlir::Operation *op) {
if (auto load = dyn_cast<fir::LoadOp>(op))
if (auto load = mlir::dyn_cast<fir::LoadOp>(op))
return !load->getAttr("volatile");
return false;
}

/// return true iff the Operation is a call
inline bool isaCall(mlir::Operation *op) {
return isa<fir::CallOp>(op) || isa<fir::DispatchOp>(op) ||
isa<mlir::func::CallOp>(op) || isa<mlir::func::CallIndirectOp>(op);
return mlir::isa<fir::CallOp>(op) || llvm::isa<fir::DispatchOp>(op) ||
mlir::isa<mlir::func::CallOp>(op) ||
mlir::isa<mlir::func::CallIndirectOp>(op);
}

/// return true iff the Operation is a fir::CallOp, fir::DispatchOp,
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Lower/Bridge.cpp
Expand Up @@ -41,6 +41,8 @@

#define DEBUG_TYPE "flang-lower-bridge"

using namespace mlir;

static llvm::cl::opt<bool> dumpBeforeFir(
"fdebug-dump-pre-fir", llvm::cl::init(false),
llvm::cl::desc("dump the Pre-FIR tree prior to FIR generation"));
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/CallInterface.cpp
Expand Up @@ -595,7 +595,7 @@ class Fortran::lower::CallInterfaceImpl {
}

void appendHostAssocTupleArg(mlir::Type tupTy) {
MLIRContext *ctxt = tupTy.getContext();
mlir::MLIRContext *ctxt = tupTy.getContext();
addFirOperand(tupTy, nextPassedArgPosition(), Property::BaseAddress,
{mlir::NamedAttribute{
mlir::StringAttr::get(ctxt, fir::getHostAssocAttrName()),
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Lower/IO.cpp
Expand Up @@ -31,6 +31,8 @@

#define DEBUG_TYPE "flang-lower-io"

using namespace mlir;

// Define additional runtime type models specific to IO.
namespace fir::runtime {
template <>
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Lower/OpenACC.cpp
Expand Up @@ -23,6 +23,8 @@
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "llvm/Frontend/OpenACC/ACC.h.inc"

using namespace mlir;

static const Fortran::parser::Name *
getDesignatorNameIfDataRef(const Fortran::parser::Designator &designator) {
const auto *dataRef{std::get_if<Fortran::parser::DataRef>(&designator.u)};
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Lower/OpenMP.cpp
Expand Up @@ -23,6 +23,8 @@
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "llvm/Frontend/OpenMP/OMPConstants.h"

using namespace mlir;

static const Fortran::parser::Name *
getDesignatorNameIfDataRef(const Fortran::parser::Designator &designator) {
const auto *dataRef = std::get_if<Fortran::parser::DataRef>(&designator.u);
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/Builder/Character.cpp
Expand Up @@ -18,6 +18,8 @@

#define DEBUG_TYPE "flang-lower-character"

using namespace mlir;

//===----------------------------------------------------------------------===//
// CharacterExprHelper implementation
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Builder/DoLoopHelper.cpp
Expand Up @@ -43,6 +43,6 @@ fir::factory::DoLoopHelper::createLoop(mlir::Value count,
auto indexType = builder.getIndexType();
auto zero = builder.createIntegerConstant(loc, indexType, 0);
auto one = builder.createIntegerConstant(loc, count.getType(), 1);
auto up = builder.create<arith::SubIOp>(loc, count, one);
auto up = builder.create<mlir::arith::SubIOp>(loc, count, one);
return createLoop(zero, up, one, bodyGenerator);
}
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Builder/FIRBuilder.cpp
Expand Up @@ -71,7 +71,7 @@ mlir::Type fir::FirOpBuilder::getRealType(int kind) {
case llvm::Type::TypeID::FP128TyID:
return mlir::FloatType::getF128(getContext());
default:
fir::emitFatalError(UnknownLoc::get(getContext()),
fir::emitFatalError(mlir::UnknownLoc::get(getContext()),
"unsupported type !fir.real<kind>");
}
}
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Optimizer/Builder/MutableBox.cpp
Expand Up @@ -586,9 +586,9 @@ void fir::factory::associateMutableBoxWithRemap(
for (auto [lb, ub] : llvm::zip(lbounds, ubounds)) {
auto lbi = builder.createConvert(loc, idxTy, lb);
auto ubi = builder.createConvert(loc, idxTy, ub);
auto diff = builder.create<arith::SubIOp>(loc, idxTy, ubi, lbi);
auto diff = builder.create<mlir::arith::SubIOp>(loc, idxTy, ubi, lbi);
extents.emplace_back(
builder.create<arith::AddIOp>(loc, idxTy, diff, one));
builder.create<mlir::arith::AddIOp>(loc, idxTy, diff, one));
}
} else {
// lbounds are default. Upper bounds and extents are the same.
Expand Down
2 changes: 0 additions & 2 deletions flang/lib/Optimizer/CodeGen/CGOps.h
Expand Up @@ -16,8 +16,6 @@
#include "flang/Optimizer/Dialect/FIRType.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"

using namespace mlir;

#define GET_OP_CLASSES
#include "flang/Optimizer/CodeGen/CGOps.h.inc"

Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Expand Up @@ -31,6 +31,8 @@

#define DEBUG_TYPE "flang-codegen"

using namespace mlir;

// fir::LLVMTypeConverter for converting to LLVM IR dialect types.
#include "TypeConverter.h"

Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
Expand Up @@ -26,6 +26,7 @@
//===----------------------------------------------------------------------===//

using namespace fir;
using namespace mlir;

#define DEBUG_TYPE "flang-codegen-rewrite"

Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/CodeGen/TargetRewrite.cpp
Expand Up @@ -30,6 +30,7 @@
#include "llvm/Support/Debug.h"

using namespace fir;
using namespace mlir;

#define DEBUG_TYPE "flang-target-rewrite"

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Dialect/FIRDialect.cpp
Expand Up @@ -42,7 +42,7 @@ struct FIRInlinerInterface : public mlir::DialectInlinerInterface {
/// return.
void handleTerminator(mlir::Operation *op,
llvm::ArrayRef<mlir::Value> valuesToRepl) const final {
auto returnOp = cast<mlir::func::ReturnOp>(op);
auto returnOp = llvm::cast<mlir::func::ReturnOp>(op);
assert(returnOp.getNumOperands() == valuesToRepl.size());
for (const auto &it : llvm::enumerate(returnOp.getOperands()))
valuesToRepl[it.index()].replaceAllUsesWith(it.value());
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Expand Up @@ -32,6 +32,7 @@ namespace {
#include "flang/Optimizer/Dialect/CanonicalizationPatterns.inc"
} // namespace
using namespace fir;
using namespace mlir;

/// Return true if a sequence type is of some incomplete size or a record type
/// is malformed or contains an incomplete sequence type. An incomplete sequence
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/AffineDemotion.cpp
Expand Up @@ -39,6 +39,7 @@
#define DEBUG_TYPE "flang-affine-demotion"

using namespace fir;
using namespace mlir;

namespace {

Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/AffinePromotion.cpp
Expand Up @@ -34,6 +34,7 @@
#define DEBUG_TYPE "flang-affine-promotion"

using namespace fir;
using namespace mlir;

namespace {
struct AffineLoopAnalysis;
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
Expand Up @@ -21,6 +21,7 @@
#define DEBUG_TYPE "flang-array-value-copy"

using namespace fir;
using namespace mlir;

using OperationUseMapT = llvm::DenseMap<mlir::Operation *, mlir::Operation *>;

Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp
Expand Up @@ -17,6 +17,8 @@
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"

using namespace mlir;

//===----------------------------------------------------------------------===//
// Helper functions
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/Transforms/MemRefDataFlowOpt.cpp
Expand Up @@ -21,6 +21,8 @@

#define DEBUG_TYPE "fir-memref-dataflow-opt"

using namespace mlir;

namespace {

template <typename OpT>
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/RewriteLoop.cpp
Expand Up @@ -18,6 +18,7 @@
#include "llvm/Support/CommandLine.h"

using namespace fir;
using namespace mlir;

namespace {

Expand Down
2 changes: 2 additions & 0 deletions flang/unittests/Optimizer/Builder/DoLoopHelperTest.cpp
Expand Up @@ -12,6 +12,8 @@
#include "flang/Optimizer/Support/KindMapping.h"
#include <string>

using namespace mlir;

struct DoLoopHelperTest : public testing::Test {
public:
void SetUp() {
Expand Down

0 comments on commit 092601d

Please sign in to comment.