-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flang] Generate main only when a Fortran program statement is present (
#89938) This patch changes the behaviour for flang to only create and link to a `main` entry point when the Fortran code has a program statement in it. This means that flang-new can be used to link even when the program is a mixed C/Fortran code with `main` present in C and no entry point present in Fortran. This also removes the `-fno-fortran-main` flag as this no longer has any functionality.
- Loading branch information
1 parent
de6b2b9
commit 8d53866
Showing
22 changed files
with
287 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===-- Main.h - generate main runtime API calls ----------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef FORTRAN_OPTIMIZER_BUILDER_RUNTIME_MAIN_H | ||
#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_MAIN_H | ||
|
||
namespace mlir { | ||
class Location; | ||
} // namespace mlir | ||
|
||
namespace fir { | ||
class FirOpBuilder; | ||
class GlobalOp; | ||
} // namespace fir | ||
|
||
namespace fir::runtime { | ||
|
||
void genMain(fir::FirOpBuilder &builder, mlir::Location loc, | ||
fir::GlobalOp &env); | ||
|
||
} | ||
|
||
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_MAIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===-- Main.cpp - generate main runtime API calls --------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "flang/Optimizer/Builder/Runtime/Main.h" | ||
#include "flang/Optimizer/Builder/BoxValue.h" | ||
#include "flang/Optimizer/Builder/FIRBuilder.h" | ||
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h" | ||
#include "flang/Optimizer/Dialect/FIROps.h" | ||
#include "flang/Optimizer/Dialect/FIRType.h" | ||
#include "flang/Runtime/main.h" | ||
#include "flang/Runtime/stop.h" | ||
|
||
using namespace Fortran::runtime; | ||
|
||
/// Create a `int main(...)` that calls the Fortran entry point | ||
void fir::runtime::genMain(fir::FirOpBuilder &builder, mlir::Location loc, | ||
fir::GlobalOp &env) { | ||
auto *context = builder.getContext(); | ||
auto argcTy = builder.getDefaultIntegerType(); | ||
auto ptrTy = mlir::LLVM::LLVMPointerType::get(context); | ||
|
||
// void ProgramStart(int argc, char** argv, char** envp, | ||
// _QQEnvironmentDefaults* env) | ||
auto startFn = builder.createFunction( | ||
loc, RTNAME_STRING(ProgramStart), | ||
mlir::FunctionType::get(context, {argcTy, ptrTy, ptrTy, ptrTy}, {})); | ||
// void ProgramStop() | ||
auto stopFn = | ||
builder.createFunction(loc, RTNAME_STRING(ProgramEndStatement), | ||
mlir::FunctionType::get(context, {}, {})); | ||
|
||
// int main(int argc, char** argv, char** envp) | ||
auto mainFn = builder.createFunction( | ||
loc, "main", | ||
mlir::FunctionType::get(context, {argcTy, ptrTy, ptrTy}, argcTy)); | ||
// void _QQmain() | ||
auto qqMainFn = builder.createFunction( | ||
loc, "_QQmain", mlir::FunctionType::get(context, {}, {})); | ||
|
||
mainFn.setPublic(); | ||
|
||
auto *block = mainFn.addEntryBlock(); | ||
mlir::OpBuilder::InsertionGuard insertGuard(builder); | ||
builder.setInsertionPointToStart(block); | ||
|
||
llvm::SmallVector<mlir::Value, 4> args(block->getArguments()); | ||
auto envAddr = | ||
builder.create<fir::AddrOfOp>(loc, env.getType(), env.getSymbol()); | ||
args.push_back(envAddr); | ||
|
||
builder.create<fir::CallOp>(loc, startFn, args); | ||
builder.create<fir::CallOp>(loc, qqMainFn); | ||
builder.create<fir::CallOp>(loc, stopFn); | ||
|
||
mlir::Value ret = builder.createIntegerConstant(loc, argcTy, 0); | ||
builder.create<mlir::func::ReturnOp>(loc, ret); | ||
} |
Oops, something went wrong.