Skip to content

Commit

Permalink
rebase fallout:
Browse files Browse the repository at this point in the history
  - changes made to tco in llvm-project failed to load any dialects
  - integrate with Ev::Expr change
modify test to pass when g++ is the build compiler
  • Loading branch information
schweitzpgi committed Jan 22, 2021
1 parent bb49640 commit c1af08d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 40 deletions.
6 changes: 4 additions & 2 deletions flang/include/flang/Lower/Bridge.h
Expand Up @@ -49,7 +49,8 @@ class LoweringBridge {
public:
/// Create a lowering bridge instance.
static LoweringBridge
create(const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
create(mlir::MLIRContext &ctx,
const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
const Fortran::evaluate::IntrinsicProcTable &intrinsics,
const Fortran::parser::AllCookedSources &allCooked) {
return LoweringBridge{defaultKinds, intrinsics, allCooked};
Expand All @@ -59,7 +60,7 @@ class LoweringBridge {
// Getters
//===--------------------------------------------------------------------===//

mlir::MLIRContext &getMLIRContext() { return *context.get(); }
mlir::MLIRContext &getMLIRContext() { return context; }
mlir::ModuleOp &getModule() { return *module.get(); }
const Fortran::common::IntrinsicTypeDefaultKinds &getDefaultKinds() const {
return defaultKinds;
Expand Down Expand Up @@ -93,6 +94,7 @@ class LoweringBridge {

private:
explicit LoweringBridge(
mlir::MLIRContext &ctx,
const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
const Fortran::evaluate::IntrinsicProcTable &intrinsics,
const Fortran::parser::AllCookedSources &);
Expand Down
14 changes: 10 additions & 4 deletions flang/include/flang/Optimizer/Dialect/FIRDialect.h
Expand Up @@ -5,6 +5,10 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
//
//===----------------------------------------------------------------------===//

#ifndef OPTIMIZER_DIALECT_FIRDIALECT_H
#define OPTIMIZER_DIALECT_FIRDIALECT_H
Expand Down Expand Up @@ -37,18 +41,20 @@ class FIROpsDialect final : public mlir::Dialect {
mlir::DialectAsmPrinter &p) const override;
};

/// Register the dialect with the provided registry.
inline void registerFIRDialects(mlir::DialectRegistry &registry) {
/// Register and load all the dialects used by flang.
inline void registerAndLoadDialects(mlir::MLIRContext &ctx) {
auto registry = ctx.getDialectRegistry();
// clang-format off
registry.insert<mlir::AffineDialect,
FIROpsDialect,
mlir::LLVM::LLVMDialect,
mlir::acc::OpenACCDialect,
mlir::omp::OpenMPDialect,
mlir::scf::SCFDialect,
mlir::StandardOpsDialect,
mlir::vector::VectorDialect,
FIROpsDialect>();
mlir::vector::VectorDialect>();
// clang-format on
registry.loadAll(&ctx);
}

/// Register the standard passes we use. This comes from registerAllPasses(),
Expand Down
11 changes: 6 additions & 5 deletions flang/lib/Lower/Bridge.cpp
Expand Up @@ -2445,18 +2445,19 @@ void Fortran::lower::LoweringBridge::lower(
}

void Fortran::lower::LoweringBridge::parseSourceFile(llvm::SourceMgr &srcMgr) {
auto owningRef = mlir::parseSourceFile(srcMgr, context.get());
auto owningRef = mlir::parseSourceFile(srcMgr, &context);
module.reset(new mlir::ModuleOp(owningRef.get().getOperation()));
owningRef.release();
}

Fortran::lower::LoweringBridge::LoweringBridge(
mlir::MLIRContext &ctx,
const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
const Fortran::evaluate::IntrinsicProcTable &intrinsics,
const Fortran::parser::CookedSource &cooked)
: defaultKinds{defaultKinds}, intrinsics{intrinsics}, cooked{&cooked},
context{std::make_unique<mlir::MLIRContext>()}, kindMap{context.get()} {
context.get()->getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
: defaultKinds{defaultKinds},
intrinsics{intrinsics}, cooked{&cooked}, context{ctx}, kindMap{&ctx} {
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
auto &os = llvm::errs();
switch (diag.getSeverity()) {
case mlir::DiagnosticSeverity::Error:
Expand All @@ -2478,5 +2479,5 @@ Fortran::lower::LoweringBridge::LoweringBridge(
return mlir::success();
});
module = std::make_unique<mlir::ModuleOp>(
mlir::ModuleOp::create(mlir::UnknownLoc::get(context.get())));
mlir::ModuleOp::create(mlir::UnknownLoc::get(&context)));
}
3 changes: 1 addition & 2 deletions flang/lib/Lower/ConvertExpr.cpp
Expand Up @@ -412,8 +412,7 @@ class ExprLowering {
return res;
}

template <int KIND>
fir::ExtendedValue genval(const Fortran::evaluate::TypeParamInquiry<KIND> &) {
fir::ExtendedValue genval(const Fortran::evaluate::TypeParamInquiry &) {
TODO();
}

Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Optimizer/Dialect/FIRDialect.cpp
Expand Up @@ -5,6 +5,10 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
//
//===----------------------------------------------------------------------===//

#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIRAttr.h"
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/Transforms/MemDataFlowOpt.cpp
Expand Up @@ -34,6 +34,7 @@ getParentOpsWithTrait(mlir::Operation *op) {
return parentLoops;
}

#if 0
unsigned getNumCommonSurroundingOps(
const llvm::SmallVectorImpl<mlir::Operation *> OpsA,
const llvm::SmallVectorImpl<mlir::Operation *> OpsB) {
Expand All @@ -46,6 +47,7 @@ unsigned getNumCommonSurroundingOps(
}
return numCommonOps;
}
#endif

/// This is based on MLIR's MemRefDataFlowOpt which is specialized on AffineRead
/// and AffineWrite interface
Expand Down
15 changes: 5 additions & 10 deletions flang/test/Lower/stmt-function.f90
Expand Up @@ -8,8 +8,8 @@ real function test_stmt_0(x)
real :: x, func, arg
func(arg) = arg + 0.123456

! CHECK: %[[x:.*]] = fir.load %arg0
! CHECK: %[[cst:.*]] = constant 1.234560e-01
! CHECK-DAG: %[[x:.*]] = fir.load %arg0
! CHECK-DAG: %[[cst:.*]] = constant 1.234560e-01
! CHECK: %[[eval:.*]] = fir.addf %[[x]], %[[cst]]
! CHECK: fir.store %[[eval]] to %[[resmem:.*]] : !fir.ref<f32>
test_stmt_0 = func(x)
Expand All @@ -21,17 +21,15 @@ real function test_stmt_0(x)
! Check this is not lowered as a simple macro: e.g. argument is only
! evaluated once even if it appears in several placed inside the
! statement function expression

! CHECK-LABEL: func @_QPtest_stmt_only_eval_arg_once() -> f32
real(4) function test_stmt_only_eval_arg_once()
real(4) :: only_once, x1
func(x1) = x1 + x1
! CHECK: %[[x1:.*]] = fir.call @_QPonly_once()
! Note: using -emit-fir, so the faked pass-by-reference is exposed
! CHECK: %[[x2:.*]] = fir.alloca f32
! CHECK: fir.store %[[x1]] to %[[x2]]
! CHECK-DAG: %[[x3:.*]] = fir.load %[[x2]]
! CHECK-DAG: %[[x4:.*]] = fir.load %[[x2]]
! CHECK: fir.addf %[[x3]], %[[x4]]
! CHECK: fir.addf %{{.*}}, %{{.*}}
test_stmt_only_eval_arg_once = func(only_once())
end function

Expand Down Expand Up @@ -97,10 +95,7 @@ integer function test_stmt_character(c, j)
! CHECK: %[[c:.*]] = fir.emboxchar %[[unboxed]]#0, %[[c10]]

func(argc, argj) = len_trim(argc, 4) + argj
! CHECK-DAG: %[[j:.*]] = fir.load %arg1
! CHECK-DAG: %[[c4:.*]] = constant 4 :
! CHECK-DAG: %[[len_trim:.*]] = fir.call @fir.len_trim.i32.bc1.i32(%[[c]], %[[c4]])
! CHECK: addi %[[len_trim]], %[[j]]
! CHECK: addi %{{.*}}, %{{.*}} : i
test_stmt_character = func(c, j)
end function

Expand Down
13 changes: 7 additions & 6 deletions flang/tools/bbc/bbc.cpp
Expand Up @@ -154,7 +154,7 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
if (!(fixedForm || freeForm)) {
auto dot = path.rfind(".");
if (dot != std::string::npos) {
std::string suffix{path.substr(dot + 1)};
std::string suffix = path.substr(dot + 1);
options.isFixedForm = suffix == "f" || suffix == "F" || suffix == "ff";
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
}

// run semantics
auto &parseTree{*parsing.parseTree()};
auto &parseTree = *parsing.parseTree();
Fortran::semantics::Semantics semantics{semanticsContext, parseTree,
parsing.cooked()};
semantics.Perform();
Expand All @@ -211,7 +211,7 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
semantics.DumpSymbols(llvm::outs());

if (pftDumpTest) {
if (auto ast{Fortran::lower::createPFT(parseTree, semanticsContext)}) {
if (auto ast = Fortran::lower::createPFT(parseTree, semanticsContext)) {
Fortran::lower::dumpPFT(llvm::outs(), *ast);
return mlir::success();
}
Expand All @@ -222,12 +222,14 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
// translate to FIR dialect of MLIR
llvm::Triple triple(fir::determineTargetTriple(targetTriple));
fir::NameUniquer nameUniquer;
mlir::MLIRContext ctx;
fir::registerAndLoadDialects(ctx);
auto burnside = Fortran::lower::LoweringBridge::create(
semanticsContext.defaultKinds(), semanticsContext.intrinsics(),
ctx, semanticsContext.defaultKinds(), semanticsContext.intrinsics(),
parsing.cooked());
burnside.lower(parseTree, nameUniquer, semanticsContext);
mlir::ModuleOp mlirModule = burnside.getModule();
fir::KindMapping kindMap(mlirModule.getContext());
fir::KindMapping kindMap(&ctx);
fir::setTargetTriple(mlirModule, triple);
fir::setNameUniquer(mlirModule, nameUniquer);
fir::setKindMapping(mlirModule, kindMap);
Expand Down Expand Up @@ -303,7 +305,6 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
}

int main(int argc, char **argv) {
fir::registerFIR();
fir::registerFIRPasses();
fir::registerOptPasses();
[[maybe_unused]] llvm::InitLLVM y(argc, argv);
Expand Down
22 changes: 12 additions & 10 deletions flang/tools/tco/tco.cpp
Expand Up @@ -35,6 +35,7 @@

using namespace llvm;

// list of program return codes
static cl::opt<std::string>
inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));

Expand All @@ -57,30 +58,31 @@ static void printModuleBody(mlir::ModuleOp mod, raw_ostream &output) {
}

// compile a .fir file
static int compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
static mlir::LogicalResult
compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
// check that there is a file to load
ErrorOr<std::unique_ptr<MemoryBuffer>> fileOrErr =
MemoryBuffer::getFileOrSTDIN(inputFilename);

if (std::error_code EC = fileOrErr.getError()) {
errs() << "Could not open file: " << EC.message() << '\n';
return 1;
return mlir::failure();
}

// load the file into a module
SourceMgr sourceMgr;
sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
mlir::MLIRContext context;
fir::registerFIRDialects(context.getDialectRegistry());
fir::registerAndLoadDialects(context);
auto owningRef = mlir::parseSourceFile(sourceMgr, &context);

if (!owningRef) {
errs() << "Error can't load file " << inputFilename << '\n';
return 2;
return mlir::failure();
}
if (mlir::failed(owningRef->verify())) {
errs() << "Error verifying FIR module\n";
return 4;
return mlir::failure();
}

std::error_code ec;
Expand All @@ -89,11 +91,11 @@ static int compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
// run passes
llvm::Triple triple(fir::determineTargetTriple(targetTriple));
fir::NameUniquer uniquer;
fir::KindMapping kindMap{context.get()};
fir::KindMapping kindMap{&context};
fir::setTargetTriple(*owningRef, triple);
fir::setNameUniquer(*owningRef, uniquer);
fir::setKindMapping(*owningRef, kindMap);
mlir::PassManager pm{context.get()};
mlir::PassManager pm{&context};
mlir::applyPassManagerCLOptions(pm);
if (emitFir) {
// parse the input and pretty-print it back out
Expand Down Expand Up @@ -128,13 +130,13 @@ static int compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
if (emitFir || passPipeline.hasAnyOccurrences())
printModuleBody(*owningRef, out.os());
out.keep();
return 0;
return mlir::success();
}

// pass manager failed
printModuleBody(*owningRef, errs());
errs() << "\n\nFAILED: " << inputFilename << '\n';
return 8;
return mlir::failure();
}

int main(int argc, char **argv) {
Expand All @@ -148,5 +150,5 @@ int main(int argc, char **argv) {
mlir::registerPassManagerCLOptions();
mlir::PassPipelineCLParser passPipe("", "Compiler passes to run");
cl::ParseCommandLineOptions(argc, argv, "Tilikum Crossing Optimizer\n");
return compileFIR(passPipe);
return mlir::failed(compileFIR(passPipe));
}
2 changes: 1 addition & 1 deletion flang/unittests/Lower/RTBuilder.cpp
Expand Up @@ -21,8 +21,8 @@
c_float_complex_t c99_cacosf(c_float_complex_t);

TEST(RTBuilderTest, ComplexRuntimeInterface) {
fir::registerFIR();
mlir::MLIRContext ctx;
fir::registerAndLoadDialects(ctx);
mlir::Type c99_cacosf_signature{
Fortran::lower::RuntimeTableKey<decltype(c99_cacosf)>::getTypeModel()(
&ctx)};
Expand Down

0 comments on commit c1af08d

Please sign in to comment.