[flang] Remove 'using namespace mlir;` from header files

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
This commit is contained in:
Andrzej Warzynski 2022-03-03 13:25:09 +00:00
parent 3d9386a349
commit 092601d4ba
31 changed files with 80 additions and 52 deletions

View File

@ -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());
}
@ -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());

View File

@ -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

View File

@ -15,8 +15,6 @@
#include "mlir/Interfaces/LoopLikeInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
using namespace mlir;
namespace fir {
class FirEndOp;

View File

@ -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));
}]
>];
@ -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();
@ -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() {
@ -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
@ -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()};
}
@ -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);
}
@ -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);
}];
}
@ -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>()";
}
@ -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.
@ -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() {

View File

@ -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,

View File

@ -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"));

View File

@ -595,7 +595,7 @@ public:
}
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()),

View File

@ -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 <>

View File

@ -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)};

View File

@ -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);

View File

@ -18,6 +18,8 @@
#define DEBUG_TYPE "flang-lower-character"
using namespace mlir;
//===----------------------------------------------------------------------===//
// CharacterExprHelper implementation
//===----------------------------------------------------------------------===//

View File

@ -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);
}

View File

@ -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>");
}
}

View File

@ -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.

View File

@ -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"

View File

@ -31,6 +31,8 @@
#define DEBUG_TYPE "flang-codegen"
using namespace mlir;
// fir::LLVMTypeConverter for converting to LLVM IR dialect types.
#include "TypeConverter.h"

View File

@ -26,6 +26,7 @@
//===----------------------------------------------------------------------===//
using namespace fir;
using namespace mlir;
#define DEBUG_TYPE "flang-codegen-rewrite"

View File

@ -30,6 +30,7 @@
#include "llvm/Support/Debug.h"
using namespace fir;
using namespace mlir;
#define DEBUG_TYPE "flang-target-rewrite"

View File

@ -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());

View File

@ -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

View File

@ -39,6 +39,7 @@
#define DEBUG_TYPE "flang-affine-demotion"
using namespace fir;
using namespace mlir;
namespace {

View File

@ -34,6 +34,7 @@
#define DEBUG_TYPE "flang-affine-promotion"
using namespace fir;
using namespace mlir;
namespace {
struct AffineLoopAnalysis;

View File

@ -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 *>;

View File

@ -17,6 +17,8 @@
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
using namespace mlir;
//===----------------------------------------------------------------------===//
// Helper functions
//===----------------------------------------------------------------------===//

View File

@ -21,6 +21,8 @@
#define DEBUG_TYPE "fir-memref-dataflow-opt"
using namespace mlir;
namespace {
template <typename OpT>

View File

@ -18,6 +18,7 @@
#include "llvm/Support/CommandLine.h"
using namespace fir;
using namespace mlir;
namespace {

View File

@ -12,6 +12,8 @@
#include "flang/Optimizer/Support/KindMapping.h"
#include <string>
using namespace mlir;
struct DoLoopHelperTest : public testing::Test {
public:
void SetUp() {

View File

@ -12,6 +12,8 @@
#include "flang/Optimizer/Support/InitFIR.h"
#include "flang/Optimizer/Support/KindMapping.h"
using namespace mlir;
struct FIRBuilderTest : public testing::Test {
public:
void SetUp() override {

View File

@ -11,6 +11,8 @@
#include "gtest/gtest.h"
#include "flang/Optimizer/Builder/Character.h"
using namespace mlir;
TEST_F(RuntimeCallTest, genAdjustLTest) {
auto loc = firBuilder->getUnknownLoc();
mlir::Value result = firBuilder->create<fir::UndefOp>(loc, boxTy);

View File

@ -10,6 +10,8 @@
#include "RuntimeCallTestBase.h"
#include "gtest/gtest.h"
using namespace mlir;
void testGenExponent(fir::FirOpBuilder &builder, mlir::Type resultType,
mlir::Type xType, llvm::StringRef fctName) {
auto loc = builder.getUnknownLoc();

View File

@ -210,8 +210,8 @@ TEST_F(RuntimeCallTest, genDotProduct) {
}
void checkGenMxxloc(fir::FirOpBuilder &builder,
void (*genFct)(fir::FirOpBuilder &, Location, mlir::Value, mlir::Value,
mlir::Value, mlir::Value, mlir::Value),
void (*genFct)(fir::FirOpBuilder &, mlir::Location, mlir::Value,
mlir::Value, mlir::Value, mlir::Value, mlir::Value),
llvm::StringRef fctName, unsigned nbArgs) {
mlir::Location loc = builder.getUnknownLoc();
mlir::Type i32Ty = builder.getI32Type();
@ -236,8 +236,8 @@ TEST_F(RuntimeCallTest, genMinlocTest) {
}
void checkGenMxxlocDim(fir::FirOpBuilder &builder,
void (*genFct)(fir::FirOpBuilder &, Location, mlir::Value, mlir::Value,
mlir::Value, mlir::Value, mlir::Value, mlir::Value),
void (*genFct)(fir::FirOpBuilder &, mlir::Location, mlir::Value,
mlir::Value, mlir::Value, mlir::Value, mlir::Value, mlir::Value),
llvm::StringRef fctName, unsigned nbArgs) {
mlir::Location loc = builder.getUnknownLoc();
auto i32Ty = builder.getI32Type();
@ -265,8 +265,8 @@ TEST_F(RuntimeCallTest, genMinlocDimTest) {
}
void checkGenMxxvalChar(fir::FirOpBuilder &builder,
void (*genFct)(
fir::FirOpBuilder &, Location, mlir::Value, mlir::Value, mlir::Value),
void (*genFct)(fir::FirOpBuilder &, mlir::Location, mlir::Value,
mlir::Value, mlir::Value),
llvm::StringRef fctName, unsigned nbArgs) {
mlir::Location loc = builder.getUnknownLoc();
auto i32Ty = builder.getI32Type();
@ -291,8 +291,8 @@ TEST_F(RuntimeCallTest, genMinvalCharTest) {
}
void checkGen4argsDim(fir::FirOpBuilder &builder,
void (*genFct)(fir::FirOpBuilder &, Location, mlir::Value, mlir::Value,
mlir::Value, mlir::Value),
void (*genFct)(fir::FirOpBuilder &, mlir::Location, mlir::Value,
mlir::Value, mlir::Value, mlir::Value),
llvm::StringRef fctName, unsigned nbArgs) {
mlir::Location loc = builder.getUnknownLoc();
auto i32Ty = builder.getI32Type();