| 1 |
//===- bbc.cpp - Burnside Bridge Compiler -----------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
/// |
| 13 |
/// This is a tool for translating Fortran sources to the FIR dialect of MLIR. |
| 14 |
/// |
| 15 |
//===----------------------------------------------------------------------===// |
| 16 |
|
| 17 |
#include "flang/Common/Fortran-features.h" |
| 18 |
#include "flang/Common/OpenMP-features.h" |
| 19 |
#include "flang/Common/Version.h" |
| 20 |
#include "flang/Common/default-kinds.h" |
| 21 |
#include "flang/Lower/Bridge.h" |
| 22 |
#include "flang/Lower/PFTBuilder.h" |
| 23 |
#include "flang/Lower/Support/Verifier.h" |
| 24 |
#include "flang/Optimizer/Dialect/Support/FIRContext.h" |
| 25 |
#include "flang/Optimizer/Dialect/Support/KindMapping.h" |
| 26 |
#include "flang/Optimizer/Support/InitFIR.h" |
| 27 |
#include "flang/Optimizer/Support/InternalNames.h" |
| 28 |
#include "flang/Optimizer/Support/Utils.h" |
| 29 |
#include "flang/Optimizer/Transforms/Passes.h" |
| 30 |
#include "flang/Parser/characters.h" |
| 31 |
#include "flang/Parser/dump-parse-tree.h" |
| 32 |
#include "flang/Parser/message.h" |
| 33 |
#include "flang/Parser/parse-tree-visitor.h" |
| 34 |
#include "flang/Parser/parse-tree.h" |
| 35 |
#include "flang/Parser/parsing.h" |
| 36 |
#include "flang/Parser/provenance.h" |
| 37 |
#include "flang/Parser/unparse.h" |
| 38 |
#include "flang/Semantics/expression.h" |
| 39 |
#include "flang/Semantics/runtime-type-info.h" |
| 40 |
#include "flang/Semantics/semantics.h" |
| 41 |
#include "flang/Semantics/unparse-with-symbols.h" |
| 42 |
#include "flang/Tools/CrossToolHelpers.h" |
| 43 |
#include "flang/Tools/TargetSetup.h" |
| 44 |
#include "flang/Version.inc" |
| 45 |
#include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 46 |
#include "mlir/IR/AsmState.h" |
| 47 |
#include "mlir/IR/BuiltinOps.h" |
| 48 |
#include "mlir/IR/MLIRContext.h" |
| 49 |
#include "mlir/Parser/Parser.h" |
| 50 |
#include "mlir/Pass/Pass.h" |
| 51 |
#include "mlir/Pass/PassManager.h" |
| 52 |
#include "mlir/Pass/PassRegistry.h" |
| 53 |
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 54 |
#include "mlir/Transforms/Passes.h" |
| 55 |
#include "llvm/MC/TargetRegistry.h" |
| 56 |
#include "llvm/Passes/OptimizationLevel.h" |
| 57 |
#include "llvm/Support/CommandLine.h" |
| 58 |
#include "llvm/Support/ErrorOr.h" |
| 59 |
#include "llvm/Support/FileSystem.h" |
| 60 |
#include "llvm/Support/InitLLVM.h" |
| 61 |
#include "llvm/Support/MemoryBuffer.h" |
| 62 |
#include "llvm/Support/Path.h" |
| 63 |
#include "llvm/Support/SourceMgr.h" |
| 64 |
#include "llvm/Support/TargetSelect.h" |
| 65 |
#include "llvm/Support/ToolOutputFile.h" |
| 66 |
#include "llvm/Support/raw_ostream.h" |
| 67 |
#include "llvm/TargetParser/Host.h" |
| 68 |
#include "llvm/TargetParser/Triple.h" |
| 69 |
#include <memory> |
| 70 |
|
| 71 |
//===----------------------------------------------------------------------===// |
| 72 |
// Some basic command-line options |
| 73 |
//===----------------------------------------------------------------------===// |
| 74 |
|
| 75 |
static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional, |
| 76 |
llvm::cl::Required, |
| 77 |
llvm::cl::desc("<input file>")); |
| 78 |
|
| 79 |
static llvm::cl::opt<std::string> |
| 80 |
outputFilename("o", llvm::cl::desc("Specify the output filename"), |
| 81 |
llvm::cl::value_desc("filename")); |
| 82 |
|
| 83 |
static llvm::cl::list<std::string> |
| 84 |
includeDirs("I", llvm::cl::desc("include module search paths")); |
| 85 |
|
| 86 |
static llvm::cl::alias includeAlias("module-directory", |
| 87 |
llvm::cl::desc("module search directory"), |
| 88 |
llvm::cl::aliasopt(includeDirs)); |
| 89 |
|
| 90 |
static llvm::cl::list<std::string> |
| 91 |
intrinsicIncludeDirs("J", llvm::cl::desc("intrinsic module search paths")); |
| 92 |
|
| 93 |
static llvm::cl::alias |
| 94 |
intrinsicIncludeAlias("intrinsic-module-directory", |
| 95 |
llvm::cl::desc("intrinsic module directory"), |
| 96 |
llvm::cl::aliasopt(intrinsicIncludeDirs)); |
| 97 |
|
| 98 |
static llvm::cl::opt<std::string> |
| 99 |
moduleDir("module", llvm::cl::desc("module output directory (default .)"), |
| 100 |
llvm::cl::init(".")); |
| 101 |
|
| 102 |
static llvm::cl::opt<std::string> |
| 103 |
moduleSuffix("module-suffix", llvm::cl::desc("module file suffix override"), |
| 104 |
llvm::cl::init(".mod")); |
| 105 |
|
| 106 |
static llvm::cl::opt<bool> |
| 107 |
emitFIR("emit-fir", |
| 108 |
llvm::cl::desc("Dump the FIR created by lowering and exit"), |
| 109 |
llvm::cl::init(false)); |
| 110 |
|
| 111 |
static llvm::cl::opt<bool> |
| 112 |
emitHLFIR("emit-hlfir", |
| 113 |
llvm::cl::desc("Dump the HLFIR created by lowering and exit"), |
| 114 |
llvm::cl::init(false)); |
| 115 |
|
| 116 |
static llvm::cl::opt<bool> warnStdViolation("Mstandard", |
| 117 |
llvm::cl::desc("emit warnings"), |
| 118 |
llvm::cl::init(false)); |
| 119 |
|
| 120 |
static llvm::cl::opt<bool> warnIsError("Werror", |
| 121 |
llvm::cl::desc("warnings are errors"), |
| 122 |
llvm::cl::init(false)); |
| 123 |
|
| 124 |
static llvm::cl::opt<bool> dumpSymbols("dump-symbols", |
| 125 |
llvm::cl::desc("dump the symbol table"), |
| 126 |
llvm::cl::init(false)); |
| 127 |
|
| 128 |
static llvm::cl::opt<bool> pftDumpTest( |
| 129 |
"pft-test", |
| 130 |
llvm::cl::desc("parse the input, create a PFT, dump it, and exit"), |
| 131 |
llvm::cl::init(false)); |
| 132 |
|
| 133 |
static llvm::cl::opt<bool> enableOpenMP("fopenmp", |
| 134 |
llvm::cl::desc("enable openmp"), |
| 135 |
llvm::cl::init(false)); |
| 136 |
|
| 137 |
static llvm::cl::opt<bool> |
| 138 |
enableOpenMPDevice("fopenmp-is-target-device", |
| 139 |
llvm::cl::desc("enable openmp device compilation"), |
| 140 |
llvm::cl::init(false)); |
| 141 |
|
| 142 |
static llvm::cl::opt<bool> |
| 143 |
enableOpenMPGPU("fopenmp-is-gpu", |
| 144 |
llvm::cl::desc("enable openmp GPU target codegen"), |
| 145 |
llvm::cl::init(false)); |
| 146 |
|
| 147 |
static llvm::cl::opt<bool> enableOpenMPForceUSM( |
| 148 |
"fopenmp-force-usm", |
| 149 |
llvm::cl::desc("force openmp unified shared memory mode"), |
| 150 |
llvm::cl::init(false)); |
| 151 |
|
| 152 |
// A simplified subset of the OpenMP RTL Flags from Flang, only the primary |
| 153 |
// positive options are available, no negative options e.g. fopen_assume* vs |
| 154 |
// fno_open_assume* |
| 155 |
static llvm::cl::opt<uint32_t> |
| 156 |
setOpenMPVersion("fopenmp-version", |
| 157 |
llvm::cl::desc("OpenMP standard version"), |
| 158 |
llvm::cl::init(11)); |
| 159 |
|
| 160 |
static llvm::cl::opt<uint32_t> setOpenMPTargetDebug( |
| 161 |
"fopenmp-target-debug", |
| 162 |
llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"), |
| 163 |
llvm::cl::init(0)); |
| 164 |
|
| 165 |
static llvm::cl::opt<bool> setOpenMPThreadSubscription( |
| 166 |
"fopenmp-assume-threads-oversubscription", |
| 167 |
llvm::cl::desc("Assume work-shared loops do not have more " |
| 168 |
"iterations than participating threads."), |
| 169 |
llvm::cl::init(false)); |
| 170 |
|
| 171 |
static llvm::cl::opt<bool> setOpenMPTeamSubscription( |
| 172 |
"fopenmp-assume-teams-oversubscription", |
| 173 |
llvm::cl::desc("Assume distributed loops do not have more iterations than " |
| 174 |
"participating teams."), |
| 175 |
llvm::cl::init(false)); |
| 176 |
|
| 177 |
static llvm::cl::opt<bool> setOpenMPNoThreadState( |
| 178 |
"fopenmp-assume-no-thread-state", |
| 179 |
llvm::cl::desc( |
| 180 |
"Assume that no thread in a parallel region will modify an ICV."), |
| 181 |
llvm::cl::init(false)); |
| 182 |
|
| 183 |
static llvm::cl::opt<bool> setOpenMPNoNestedParallelism( |
| 184 |
"fopenmp-assume-no-nested-parallelism", |
| 185 |
llvm::cl::desc("Assume that no thread in a parallel region will encounter " |
| 186 |
"a parallel region."), |
| 187 |
llvm::cl::init(false)); |
| 188 |
|
| 189 |
static llvm::cl::opt<bool> |
| 190 |
setNoGPULib("nogpulib", |
| 191 |
llvm::cl::desc("Do not link device library for CUDA/HIP device " |
| 192 |
"compilation"), |
| 193 |
llvm::cl::init(false)); |
| 194 |
|
| 195 |
static llvm::cl::opt<bool> enableOpenACC("fopenacc", |
| 196 |
llvm::cl::desc("enable openacc"), |
| 197 |
llvm::cl::init(false)); |
| 198 |
|
| 199 |
static llvm::cl::opt<bool> enableNoPPCNativeVecElemOrder( |
| 200 |
"fno-ppc-native-vector-element-order", |
| 201 |
llvm::cl::desc("no PowerPC native vector element order."), |
| 202 |
llvm::cl::init(false)); |
| 203 |
|
| 204 |
static llvm::cl::opt<bool> useHLFIR("hlfir", |
| 205 |
llvm::cl::desc("Lower to high level FIR"), |
| 206 |
llvm::cl::init(true)); |
| 207 |
|
| 208 |
static llvm::cl::opt<bool> enableCUDA("fcuda", |
| 209 |
llvm::cl::desc("enable CUDA Fortran"), |
| 210 |
llvm::cl::init(false)); |
| 211 |
|
| 212 |
static llvm::cl::opt<std::string> |
| 213 |
enableGPUMode("gpu", llvm::cl::desc("Enable GPU Mode managed|unified"), |
| 214 |
llvm::cl::init("")); |
| 215 |
|
| 216 |
static llvm::cl::opt<bool> fixedForm("ffixed-form", |
| 217 |
llvm::cl::desc("enable fixed form"), |
| 218 |
llvm::cl::init(false)); |
| 219 |
static llvm::cl::opt<std::string> |
| 220 |
targetTripleOverride("target", |
| 221 |
llvm::cl::desc("Override host target triple"), |
| 222 |
llvm::cl::init("")); |
| 223 |
|
| 224 |
static llvm::cl::opt<bool> |
| 225 |
setNSW("integer-overflow", |
| 226 |
llvm::cl::desc("add nsw flag to internal operations"), |
| 227 |
llvm::cl::init(false)); |
| 228 |
|
| 229 |
#define FLANG_EXCLUDE_CODEGEN |
| 230 |
#include "flang/Tools/CLOptions.inc" |
| 231 |
|
| 232 |
//===----------------------------------------------------------------------===// |
| 233 |
|
| 234 |
using ProgramName = std::string; |
| 235 |
|
| 236 |
// Print the module with the "module { ... }" wrapper, preventing |
| 237 |
// information loss from attribute information appended to the module |
| 238 |
static void printModule(mlir::ModuleOp mlirModule, llvm::raw_ostream &out) { |
| 239 |
out << mlirModule << '\n'; |
| 240 |
} |
| 241 |
|
| 242 |
static void registerAllPasses() { |
| 243 |
fir::support::registerMLIRPassesForFortranTools(); |
| 244 |
fir::registerOptTransformPasses(); |
| 245 |
} |
| 246 |
|
| 247 |
/// Create a target machine that is at least sufficient to get data-layout |
| 248 |
/// information required by flang semantics and lowering. Note that it may not |
| 249 |
/// contain all the CPU feature information to get optimized assembly generation |
| 250 |
/// from LLVM IR. Drivers that needs to generate assembly from LLVM IR should |
| 251 |
/// create a target machine according to their specific options. |
| 252 |
static std::unique_ptr<llvm::TargetMachine> |
| 253 |
createTargetMachine(llvm::StringRef targetTriple, std::string &error) { |
| 254 |
std::string triple{targetTriple}; |
| 255 |
if (triple.empty()) |
| 256 |
triple = llvm::sys::getDefaultTargetTriple(); |
| 257 |
|
| 258 |
const llvm::Target *theTarget = |
| 259 |
llvm::TargetRegistry::lookupTarget(triple, error); |
| 260 |
if (!theTarget) |
| 261 |
return nullptr; |
| 262 |
return std::unique_ptr<llvm::TargetMachine>{ |
| 263 |
theTarget->createTargetMachine(triple, /*CPU=*/"", |
| 264 |
/*Features=*/"", llvm::TargetOptions(), |
| 265 |
/*Reloc::Model=*/std::nullopt)}; |
| 266 |
} |
| 267 |
|
| 268 |
/// Build and execute the OpenMPFIRPassPipeline with its own instance |
| 269 |
/// of the pass manager, allowing it to be invoked as soon as it's |
| 270 |
/// required without impacting the main pass pipeline that may be invoked |
| 271 |
/// more than once for verification. |
| 272 |
static llvm::LogicalResult runOpenMPPasses(mlir::ModuleOp mlirModule) { |
| 273 |
mlir::PassManager pm(mlirModule->getName(), |
| 274 |
mlir::OpPassManager::Nesting::Implicit); |
| 275 |
fir::createOpenMPFIRPassPipeline(pm, enableOpenMPDevice); |
| 276 |
(void)mlir::applyPassManagerCLOptions(pm); |
| 277 |
if (mlir::failed(pm.run(mlirModule))) { |
| 278 |
llvm::errs() << "FATAL: failed to correctly apply OpenMP pass pipeline"; |
| 279 |
return mlir::failure(); |
| 280 |
} |
| 281 |
return mlir::success(); |
| 282 |
} |
| 283 |
|
| 284 |
//===----------------------------------------------------------------------===// |
| 285 |
// Translate Fortran input to FIR, a dialect of MLIR. |
| 286 |
//===----------------------------------------------------------------------===// |
| 287 |
|
| 288 |
static llvm::LogicalResult convertFortranSourceToMLIR( |
| 289 |
std::string path, Fortran::parser::Options options, |
| 290 |
const ProgramName &programPrefix, |
| 291 |
Fortran::semantics::SemanticsContext &semanticsContext, |
| 292 |
const mlir::PassPipelineCLParser &passPipeline, |
| 293 |
const llvm::TargetMachine &targetMachine) { |
| 294 |
|
| 295 |
// prep for prescan and parse |
| 296 |
Fortran::parser::Parsing parsing{semanticsContext.allCookedSources()}; |
| 297 |
parsing.Prescan(path, options); |
| 298 |
if (!parsing.messages().empty() && (parsing.messages().AnyFatalError())) { |
| 299 |
llvm::errs() << programPrefix << "could not scan " << path << '\n'; |
| 300 |
parsing.messages().Emit(llvm::errs(), parsing.allCooked()); |
| 301 |
return mlir::failure(); |
| 302 |
} |
| 303 |
|
| 304 |
// parse the input Fortran |
| 305 |
parsing.Parse(llvm::outs()); |
| 306 |
if (!parsing.consumedWholeFile()) { |
| 307 |
parsing.messages().Emit(llvm::errs(), parsing.allCooked()); |
| 308 |
parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(), |
| 309 |
"parser FAIL (final position)", |
| 310 |
"error: ", llvm::raw_ostream::RED); |
| 311 |
return mlir::failure(); |
| 312 |
} else if ((!parsing.messages().empty() && |
| 313 |
(parsing.messages().AnyFatalError())) || |
| 314 |
!parsing.parseTree().has_value()) { |
| 315 |
parsing.messages().Emit(llvm::errs(), parsing.allCooked()); |
| 316 |
llvm::errs() << programPrefix << "could not parse " << path << '\n'; |
| 317 |
return mlir::failure(); |
| 318 |
} else { |
| 319 |
semanticsContext.messages().Annex(std::move(parsing.messages())); |
| 320 |
} |
| 321 |
|
| 322 |
// run semantics |
| 323 |
auto &parseTree = *parsing.parseTree(); |
| 324 |
Fortran::semantics::Semantics semantics(semanticsContext, parseTree); |
| 325 |
semantics.Perform(); |
| 326 |
semantics.EmitMessages(llvm::errs()); |
| 327 |
if (semantics.AnyFatalError()) { |
| 328 |
llvm::errs() << programPrefix << "semantic errors in " << path << '\n'; |
| 329 |
return mlir::failure(); |
| 330 |
} |
| 331 |
Fortran::semantics::RuntimeDerivedTypeTables tables; |
| 332 |
if (!semantics.AnyFatalError()) { |
| 333 |
tables = |
| 334 |
Fortran::semantics::BuildRuntimeDerivedTypeTables(semanticsContext); |
| 335 |
if (!tables.schemata) |
| 336 |
llvm::errs() << programPrefix |
| 337 |
<< "could not find module file for __fortran_type_info\n"; |
| 338 |
} |
| 339 |
|
| 340 |
if (dumpSymbols) { |
| 341 |
semantics.DumpSymbols(llvm::outs()); |
| 342 |
return mlir::success(); |
| 343 |
} |
| 344 |
|
| 345 |
if (pftDumpTest) { |
| 346 |
if (auto ast = Fortran::lower::createPFT(parseTree, semanticsContext)) { |
| 347 |
Fortran::lower::dumpPFT(llvm::outs(), *ast); |
| 348 |
return mlir::success(); |
| 349 |
} |
| 350 |
llvm::errs() << "Pre FIR Tree is NULL.\n"; |
| 351 |
return mlir::failure(); |
| 352 |
} |
| 353 |
|
| 354 |
// translate to FIR dialect of MLIR |
| 355 |
mlir::DialectRegistry registry; |
| 356 |
fir::support::registerNonCodegenDialects(registry); |
| 357 |
fir::support::addFIRExtensions(registry); |
| 358 |
mlir::MLIRContext ctx(registry); |
| 359 |
fir::support::loadNonCodegenDialects(ctx); |
| 360 |
auto &defKinds = semanticsContext.defaultKinds(); |
| 361 |
fir::KindMapping kindMap( |
| 362 |
&ctx, llvm::ArrayRef<fir::KindTy>{fir::fromDefaultKinds(defKinds)}); |
| 363 |
std::string targetTriple = targetMachine.getTargetTriple().normalize(); |
| 364 |
// Use default lowering options for bbc. |
| 365 |
Fortran::lower::LoweringOptions loweringOptions{}; |
| 366 |
loweringOptions.setNoPPCNativeVecElemOrder(enableNoPPCNativeVecElemOrder); |
| 367 |
loweringOptions.setLowerToHighLevelFIR(useHLFIR || emitHLFIR); |
| 368 |
loweringOptions.setNSWOnLoopVarInc(setNSW); |
| 369 |
std::vector<Fortran::lower::EnvironmentDefault> envDefaults = {}; |
| 370 |
constexpr const char *tuneCPU = ""; |
| 371 |
auto burnside = Fortran::lower::LoweringBridge::create( |
| 372 |
ctx, semanticsContext, defKinds, semanticsContext.intrinsics(), |
| 373 |
semanticsContext.targetCharacteristics(), parsing.allCooked(), |
| 374 |
targetTriple, kindMap, loweringOptions, envDefaults, |
| 375 |
semanticsContext.languageFeatures(), targetMachine, tuneCPU); |
| 376 |
mlir::ModuleOp mlirModule = burnside.getModule(); |
| 377 |
if (enableOpenMP) { |
| 378 |
if (enableOpenMPGPU && !enableOpenMPDevice) { |
| 379 |
llvm::errs() << "FATAL: -fopenmp-is-gpu can only be set if " |
| 380 |
"-fopenmp-is-target-device is also set"; |
| 381 |
return mlir::failure(); |
| 382 |
} |
| 383 |
auto offloadModuleOpts = OffloadModuleOpts( |
| 384 |
setOpenMPTargetDebug, setOpenMPTeamSubscription, |
| 385 |
setOpenMPThreadSubscription, setOpenMPNoThreadState, |
| 386 |
setOpenMPNoNestedParallelism, enableOpenMPDevice, enableOpenMPGPU, |
| 387 |
enableOpenMPForceUSM, setOpenMPVersion, "", setNoGPULib); |
| 388 |
setOffloadModuleInterfaceAttributes(mlirModule, offloadModuleOpts); |
| 389 |
setOpenMPVersionAttribute(mlirModule, setOpenMPVersion); |
| 390 |
} |
| 391 |
burnside.lower(parseTree, semanticsContext); |
| 392 |
std::error_code ec; |
| 393 |
std::string outputName = outputFilename; |
| 394 |
if (!outputName.size()) |
| 395 |
outputName = llvm::sys::path::stem(inputFilename).str().append(".mlir"); |
| 396 |
llvm::raw_fd_ostream out(outputName, ec); |
| 397 |
if (ec) |
| 398 |
return mlir::emitError(mlir::UnknownLoc::get(&ctx), |
| 399 |
"could not open output file ") |
| 400 |
<< outputName; |
| 401 |
|
| 402 |
// WARNING: This pipeline must be run immediately after the lowering to |
| 403 |
// ensure that the FIR is correct with respect to OpenMP operations/ |
| 404 |
// attributes. |
| 405 |
if (enableOpenMP) |
| 406 |
if (mlir::failed(runOpenMPPasses(mlirModule))) |
| 407 |
return mlir::failure(); |
| 408 |
|
| 409 |
// Otherwise run the default passes. |
| 410 |
mlir::PassManager pm(mlirModule->getName(), |
| 411 |
mlir::OpPassManager::Nesting::Implicit); |
| 412 |
pm.enableVerifier(/*verifyPasses=*/true); |
| 413 |
(void)mlir::applyPassManagerCLOptions(pm); |
| 414 |
if (passPipeline.hasAnyOccurrences()) { |
| 415 |
// run the command-line specified pipeline |
| 416 |
hlfir::registerHLFIRPasses(); |
| 417 |
(void)passPipeline.addToPipeline(pm, [&](const llvm::Twine &msg) { |
| 418 |
mlir::emitError(mlir::UnknownLoc::get(&ctx)) << msg; |
| 419 |
return mlir::failure(); |
| 420 |
}); |
| 421 |
} else if (emitFIR || emitHLFIR) { |
| 422 |
// --emit-fir: Build the IR, verify it, and dump the IR if the IR passes |
| 423 |
// verification. Use --dump-module-on-failure to dump invalid IR. |
| 424 |
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>()); |
| 425 |
if (mlir::failed(pm.run(mlirModule))) { |
| 426 |
llvm::errs() << "FATAL: verification of lowering to FIR failed"; |
| 427 |
return mlir::failure(); |
| 428 |
} |
| 429 |
|
| 430 |
if (emitFIR && useHLFIR) { |
| 431 |
// lower HLFIR to FIR |
| 432 |
fir::createHLFIRToFIRPassPipeline(pm, llvm::OptimizationLevel::O2); |
| 433 |
if (mlir::failed(pm.run(mlirModule))) { |
| 434 |
llvm::errs() << "FATAL: lowering from HLFIR to FIR failed"; |
| 435 |
return mlir::failure(); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
printModule(mlirModule, out); |
| 440 |
return mlir::success(); |
| 441 |
} else { |
| 442 |
// run the default canned pipeline |
| 443 |
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>()); |
| 444 |
|
| 445 |
// Add O2 optimizer pass pipeline. |
| 446 |
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2); |
| 447 |
config.NSWOnLoopVarInc = setNSW; |
| 448 |
fir::registerDefaultInlinerPass(config); |
| 449 |
fir::createDefaultFIROptimizerPassPipeline(pm, config); |
| 450 |
} |
| 451 |
|
| 452 |
if (mlir::succeeded(pm.run(mlirModule))) { |
| 453 |
// Emit MLIR and do not lower to LLVM IR. |
| 454 |
printModule(mlirModule, out); |
| 455 |
return mlir::success(); |
| 456 |
} |
| 457 |
// Something went wrong. Try to dump the MLIR module. |
| 458 |
llvm::errs() << "oops, pass manager reported failure\n"; |
| 459 |
return mlir::failure(); |
| 460 |
} |
| 461 |
|
| 462 |
int main(int argc, char **argv) { |
| 463 |
[[maybe_unused]] llvm::InitLLVM y(argc, argv); |
| 464 |
llvm::InitializeAllTargets(); |
| 465 |
llvm::InitializeAllTargetMCs(); |
| 466 |
registerAllPasses(); |
| 467 |
|
| 468 |
mlir::registerMLIRContextCLOptions(); |
| 469 |
mlir::registerAsmPrinterCLOptions(); |
| 470 |
mlir::registerPassManagerCLOptions(); |
| 471 |
mlir::PassPipelineCLParser passPipe("", "Compiler passes to run"); |
| 472 |
llvm::cl::ParseCommandLineOptions(argc, argv, "Burnside Bridge Compiler\n"); |
| 473 |
|
| 474 |
ProgramName programPrefix; |
| 475 |
programPrefix = argv[0] + ": "s; |
| 476 |
|
| 477 |
if (includeDirs.size() == 0) { |
| 478 |
includeDirs.push_back("."); |
| 479 |
// Default Fortran modules should be installed in include/flang (a sibling |
| 480 |
// to the bin) directory. |
| 481 |
intrinsicIncludeDirs.push_back( |
| 482 |
llvm::sys::path::parent_path( |
| 483 |
llvm::sys::path::parent_path( |
| 484 |
llvm::sys::fs::getMainExecutable(argv[0], nullptr))) |
| 485 |
.str() + |
| 486 |
"/include/flang"); |
| 487 |
} |
| 488 |
|
| 489 |
Fortran::parser::Options options; |
| 490 |
options.predefinitions.emplace_back("__flang__"s, "1"s); |
| 491 |
options.predefinitions.emplace_back("__flang_major__"s, |
| 492 |
std::string{FLANG_VERSION_MAJOR_STRING}); |
| 493 |
options.predefinitions.emplace_back("__flang_minor__"s, |
| 494 |
std::string{FLANG_VERSION_MINOR_STRING}); |
| 495 |
options.predefinitions.emplace_back( |
| 496 |
"__flang_patchlevel__"s, std::string{FLANG_VERSION_PATCHLEVEL_STRING}); |
| 497 |
|
| 498 |
// enable parsing of OpenMP |
| 499 |
if (enableOpenMP) { |
| 500 |
options.features.Enable(Fortran::common::LanguageFeature::OpenMP); |
| 501 |
Fortran::common::setOpenMPMacro(setOpenMPVersion, options.predefinitions); |
| 502 |
} |
| 503 |
|
| 504 |
// enable parsing of OpenACC |
| 505 |
if (enableOpenACC) { |
| 506 |
options.features.Enable(Fortran::common::LanguageFeature::OpenACC); |
| 507 |
options.predefinitions.emplace_back("_OPENACC", "202211"); |
| 508 |
} |
| 509 |
|
| 510 |
// enable parsing of CUDA Fortran |
| 511 |
if (enableCUDA) { |
| 512 |
options.features.Enable(Fortran::common::LanguageFeature::CUDA); |
| 513 |
} |
| 514 |
|
| 515 |
if (enableGPUMode == "managed") { |
| 516 |
options.features.Enable(Fortran::common::LanguageFeature::CudaManaged); |
| 517 |
} else if (enableGPUMode == "unified") { |
| 518 |
options.features.Enable(Fortran::common::LanguageFeature::CudaUnified); |
| 519 |
} |
| 520 |
|
| 521 |
if (fixedForm) { |
| 522 |
options.isFixedForm = fixedForm; |
| 523 |
} |
| 524 |
|
| 525 |
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds; |
| 526 |
Fortran::parser::AllSources allSources; |
| 527 |
Fortran::parser::AllCookedSources allCookedSources(allSources); |
| 528 |
Fortran::semantics::SemanticsContext semanticsContext{ |
| 529 |
defaultKinds, options.features, allCookedSources}; |
| 530 |
semanticsContext.set_moduleDirectory(moduleDir) |
| 531 |
.set_moduleFileSuffix(moduleSuffix) |
| 532 |
.set_searchDirectories(includeDirs) |
| 533 |
.set_intrinsicModuleDirectories(intrinsicIncludeDirs) |
| 534 |
.set_warnOnNonstandardUsage(warnStdViolation) |
| 535 |
.set_warningsAreErrors(warnIsError); |
| 536 |
|
| 537 |
std::string error; |
| 538 |
// Create host target machine. |
| 539 |
std::unique_ptr<llvm::TargetMachine> targetMachine = |
| 540 |
createTargetMachine(targetTripleOverride, error); |
| 541 |
if (!targetMachine) { |
| 542 |
llvm::errs() << "failed to create target machine: " << error << "\n"; |
| 543 |
return mlir::failed(mlir::failure()); |
| 544 |
} |
| 545 |
std::string compilerVersion = Fortran::common::getFlangToolFullVersion("bbc"); |
| 546 |
std::string compilerOptions = ""; |
| 547 |
Fortran::tools::setUpTargetCharacteristics( |
| 548 |
semanticsContext.targetCharacteristics(), *targetMachine, compilerVersion, |
| 549 |
compilerOptions); |
| 550 |
|
| 551 |
return mlir::failed( |
| 552 |
convertFortranSourceToMLIR(inputFilename, options, programPrefix, |
| 553 |
semanticsContext, passPipe, *targetMachine)); |
| 554 |
} |