[clang] [flang] [flang] Add -mlink-builtin-bitcode option to fc1 (PR #94763)
Jan Leyonberg via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 11 07:23:15 PDT 2024
https://github.com/jsjodin updated https://github.com/llvm/llvm-project/pull/94763
>From adfeb69ba312e971cb4eae71f185f38b1cac9b8f Mon Sep 17 00:00:00 2001
From: Jan Leyonberg <jan_sjodin at yahoo.com>
Date: Wed, 5 Jun 2024 10:56:41 -0400
Subject: [PATCH 1/2] [flang] Add -mlink-builtin-bitcode option to fc1
This patch enables the -mlink-builtin-bitcode flag in fc1 so that bitcode
libraries can be linked in. This is needed for OpenMP offloading libraries.
---
clang/include/clang/Driver/Options.td | 9 ++-
flang/include/flang/Frontend/CodeGenOptions.h | 4 ++
.../include/flang/Frontend/FrontendActions.h | 5 +-
flang/lib/Frontend/CompilerInvocation.cpp | 5 ++
flang/lib/Frontend/FrontendActions.cpp | 56 ++++++++++++++++++
flang/test/Driver/Inputs/bclib.bc | Bin 0 -> 1700 bytes
flang/test/Driver/mlink-builtin-bc.f90 | 18 ++++++
7 files changed, 93 insertions(+), 4 deletions(-)
create mode 100644 flang/test/Driver/Inputs/bclib.bc
create mode 100644 flang/test/Driver/mlink-builtin-bc.f90
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index d44faa55c456f..490538ce753e0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7028,6 +7028,12 @@ def as_secure_log_file : Separate<["-"], "as-secure-log-file">,
} // let Visibility = [CC1Option, CC1AsOption]
+let Visibility = [CC1Option, FC1Option] in {
+def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
+ HelpText<"Link and internalize needed symbols from the given bitcode file "
+ "before performing optimizations.">;
+} // let Visibility = [CC1Option, FC1Option]
+
let Visibility = [CC1Option] in {
def llvm_verify_each : Flag<["-"], "llvm-verify-each">,
@@ -7130,9 +7136,6 @@ defm constructor_aliases : BoolMOption<"constructor-aliases",
" emitting complete constructors and destructors as aliases when possible">>;
def mlink_bitcode_file : Separate<["-"], "mlink-bitcode-file">,
HelpText<"Link the given bitcode file before performing optimizations.">;
-def mlink_builtin_bitcode : Separate<["-"], "mlink-builtin-bitcode">,
- HelpText<"Link and internalize needed symbols from the given bitcode file "
- "before performing optimizations.">;
defm link_builtin_bitcode_postopt: BoolMOption<"link-builtin-bitcode-postopt",
CodeGenOpts<"LinkBitcodePostopt">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption], "Link builtin bitcodes after the "
diff --git a/flang/include/flang/Frontend/CodeGenOptions.h b/flang/include/flang/Frontend/CodeGenOptions.h
index 918192abae724..3bc5d93c4c43e 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.h
+++ b/flang/include/flang/Frontend/CodeGenOptions.h
@@ -56,6 +56,10 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// are offloading binaries containing device images and metadata.
std::vector<std::string> OffloadObjects;
+ /// List of filenames passed in using the -mlink-builtin-bitcode. These
+ /// are bc libraries that should be linked in and internalized;
+ std::vector<std::string> BuiltinBCLibs;
+
/// The directory where temp files are stored if specified by -save-temps
std::optional<std::string> SaveTempsDir;
diff --git a/flang/include/flang/Frontend/FrontendActions.h b/flang/include/flang/Frontend/FrontendActions.h
index 7823565eb815f..0a9a9c3401d1d 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -223,9 +223,12 @@ class CodeGenAction : public FrontendAction {
std::unique_ptr<llvm::LLVMContext> llvmCtx;
std::unique_ptr<llvm::Module> llvmModule;
- /// Embeds offload objects given with specified with -fembed-offload-object
+ /// Embeds offload objects specified with -fembed-offload-object
void embedOffloadObjects();
+ /// Links in BC libraries spefified with -fmlink-builtin-bitcode
+ void linkBuiltinBCLibs();
+
/// Runs pass pipeline to lower HLFIR into FIR
void lowerHLFIRToFIR();
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f64a939b785ef..f96d72f1ad691 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -347,6 +347,11 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
if (auto *a = args.getLastArg(clang::driver::options::OPT_save_temps_EQ))
opts.SaveTempsDir = a->getValue();
+ // -mlink-builtin-bitcode
+ for (auto *a :
+ args.filtered(clang::driver::options::OPT_mlink_builtin_bitcode))
+ opts.BuiltinBCLibs.push_back(a->getValue());
+
// -mrelocation-model option.
if (const llvm::opt::Arg *a =
args.getLastArg(clang::driver::options::OPT_mrelocation_model)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index b1b6391f1439c..c2e6af58ffd79 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -43,6 +43,8 @@
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/FileSystemOptions.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
@@ -54,6 +56,7 @@
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
+#include "llvm/Linker/Linker.h"
#include "llvm/Object/OffloadBinary.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
@@ -68,6 +71,7 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/RISCVISAInfo.h"
#include "llvm/TargetParser/RISCVTargetParser.h"
+#include "llvm/Transforms/IPO/Internalize.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <memory>
#include <system_error>
@@ -1146,6 +1150,54 @@ void CodeGenAction::embedOffloadObjects() {
}
}
+void CodeGenAction::linkBuiltinBCLibs() {
+ auto options = clang::FileSystemOptions();
+ clang::FileManager fileManager(options);
+ CompilerInstance &ci = this->getInstance();
+ const auto &cgOpts = ci.getInvocation().getCodeGenOpts();
+
+ std::vector<std::unique_ptr<llvm::Module>> modules;
+
+ // Load LLVM modules
+ for (llvm::StringRef bcLib : cgOpts.BuiltinBCLibs) {
+ auto BCBuf = fileManager.getBufferForFile(bcLib);
+ if (!BCBuf) {
+ auto diagID = ci.getDiagnostics().getCustomDiagID(
+ clang::DiagnosticsEngine::Error, "could not open '%0' for linking");
+ ci.getDiagnostics().Report(diagID) << bcLib;
+ return;
+ }
+
+ llvm::Expected<std::unique_ptr<llvm::Module>> ModuleOrErr =
+ getOwningLazyBitcodeModule(std::move(*BCBuf), *llvmCtx);
+ if (!ModuleOrErr) {
+ auto diagID = ci.getDiagnostics().getCustomDiagID(
+ clang::DiagnosticsEngine::Error, "error loading '%0' for linking");
+ ci.getDiagnostics().Report(diagID) << bcLib;
+ return;
+ }
+ modules.push_back(std::move(ModuleOrErr.get()));
+ }
+
+ // Link modules and internalize functions
+ for (auto &module : modules) {
+ bool Err;
+ Err = llvm::Linker::linkModules(
+ *llvmModule, std::move(module), llvm::Linker::Flags::LinkOnlyNeeded,
+ [](llvm::Module &M, const llvm::StringSet<> &GVS) {
+ llvm::internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
+ return !GV.hasName() || (GVS.count(GV.getName()) == 0);
+ });
+ });
+ if (Err) {
+ auto diagID = ci.getDiagnostics().getCustomDiagID(
+ clang::DiagnosticsEngine::Error, "link error when linking '%0'");
+ ci.getDiagnostics().Report(diagID) << module->getSourceFileName();
+ return;
+ }
+ }
+}
+
static void reportOptRecordError(llvm::Error e, clang::DiagnosticsEngine &diags,
const CodeGenOptions &codeGenOpts) {
handleAllErrors(
@@ -1237,6 +1289,10 @@ void CodeGenAction::executeAction() {
llvmModule->setTargetTriple(theTriple);
llvmModule->setDataLayout(targetMachine.createDataLayout());
+ // Link in builtin bitcode libraries
+ if (!codeGenOpts.BuiltinBCLibs.empty())
+ linkBuiltinBCLibs();
+
// Embed offload objects specified with -fembed-offload-object
if (!codeGenOpts.OffloadObjects.empty())
embedOffloadObjects();
diff --git a/flang/test/Driver/Inputs/bclib.bc b/flang/test/Driver/Inputs/bclib.bc
new file mode 100644
index 0000000000000000000000000000000000000000..bf97a6cd3dbe10bc560b1067834ae945ac700e94
GIT binary patch
literal 1700
zcmX{WT})GF^p--u3iZ2piWBtSdat12VYqAgvDe!s(5nl}Tvpv4x?~F!p~6IVDTS75
zmX at +z%=%Cb4?MV}Y(D7I(DC3BvQRE_6|3vavXG3CcKiqn0-0<vnb~%3J9m<EzVn^)
zedjyBpF^5js4FFiB7z`VO?>5>?|$R?<*$ji_9rV(=qyD9X9Bs7AW8~2q5#g61EO%n
zmBwV@>jugqT-6Pk4iy>8&4r(~hD;|599K&mZIq?dYAk!B*kT!Ol4rPz6T6fs)p)g_
zEor<}%1W;dv-fls%a9Alu5c$}V}qX;l&k3eo*kBG^bo}y_azD9GD{FU;H`t6Lh80-
z at E&yrNUUet*;KsauynV`VL5Cx?$cE{c#7D~Uz*a3&x*u&r2Yo-yO2MiT$Gji7R4tk
z!6~KQiu^Z}onJcfg)|NVB}lJ~<T&bOQIJR8v|)=yek#Z3g!u#BIO=ae!4~988`gm4
zwijZ!hed%n(rDVS64LDC`Hc_?Qm8*~Hh1eQ3VB0lA7{E;I6YYS_ifY7kOVRP4_4yx
z&VrK<Ag`@QL8t=O7(?_-miCnD?8B0ENR2u|Xdr-kM|(^w1o?!*+aX+Ia+s$R%)>F-
z!x!a_MfRuk_M~Kw>g}mO)F-2hMl{f*$TAAb`u>0-vr2jDCA=QPS)T0X$;B<a<-|4A
z470$|a~w16HhcKX;$%cjOTvWSHW{%E>BZDmX at 3j4*ny69C|;l;MUE+Q3+k^z{m+WP
ztUshQ8<n0i#XG7DjD}y?*uonb{Kpht=k=}0F?wl?PUYylo1W$9nJ_c!HvANmgrp>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M#U0wO-h<^e3E-~x?
literal 0
HcmV?d00001
diff --git a/flang/test/Driver/mlink-builtin-bc.f90 b/flang/test/Driver/mlink-builtin-bc.f90
new file mode 100644
index 0000000000000..0d655a24a7a63
--- /dev/null
+++ b/flang/test/Driver/mlink-builtin-bc.f90
@@ -0,0 +1,18 @@
+
+!----------
+! RUN lines
+!----------
+! Embed something that can be easily checked
+! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - -mlink-builtin-bitcode %S/Inputs/bclib.bc %s 2>&1 | FileCheck %s
+
+! CHECK: define internal void @libfun_
+
+! RUN1: not %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - -mlink-builtin-bitcode %S/Inputs/no-bclib.bc %s 2>&1 | FileCheck %s
+
+! ERROR1: error: could not open {{.*}} no-bclib.bc
+
+external libfun
+parameter(i=1)
+integer :: j
+call libfun(j)
+end program
>From 2b348b3ec258d444c1fb1a008744d44887a713e1 Mon Sep 17 00:00:00 2001
From: Jan Leyonberg <jan_sjodin at yahoo.com>
Date: Tue, 11 Jun 2024 10:22:30 -0400
Subject: [PATCH 2/2] Use fortran source file to create bc library instead of
using .bc file.
---
flang/test/Driver/Inputs/bclib.bc | Bin 1700 -> 0 bytes
flang/test/Driver/Inputs/libfun.f90 | 4 ++++
flang/test/Driver/mlink-builtin-bc.f90 | 13 +++++--------
3 files changed, 9 insertions(+), 8 deletions(-)
delete mode 100644 flang/test/Driver/Inputs/bclib.bc
create mode 100644 flang/test/Driver/Inputs/libfun.f90
diff --git a/flang/test/Driver/Inputs/bclib.bc b/flang/test/Driver/Inputs/bclib.bc
deleted file mode 100644
index bf97a6cd3dbe10bc560b1067834ae945ac700e94..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001
literal 1700
zcmX{WT})GF^p--u3iZ2piWBtSdat12VYqAgvDe!s(5nl}Tvpv4x?~F!p~6IVDTS75
zmX at +z%=%Cb4?MV}Y(D7I(DC3BvQRE_6|3vavXG3CcKi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%*<u?u5-)^$INgHRB)9$;9)-#CnftGy)Y6HQm#p;J_H*CKbsW4td!q>j5m2Q
zo55=h<XRkFQFZ!Wn3po8c@$bj%djsEr3?=>3K<nrXNO9q8fN0m>==`2G%qA*Pvu!2
zXSMp0U}Tw>xJX|a>TOb*<I1HF+`ToTvqe?gh-yts$Dy54kTFvex5wc&gR2h{%$abh
z=hQhKZ?fcO#<1xmw>49EIb&F1$!r>e{h}e#-+=~<h`3os5Y;dfCB at z<=rt5{$)gaT
z=9-XJ2UqC3V>?$HwNU`u`5L}lyV!ysJE1kmC9Ofo$4<OG9txv at s$Ay=6v(<(H<x@(
zwNB{7>k<2iT5ukQfS%9N^DMpO*63>3Jng2_V{|f~Y<EIOZi9p1ZwQwR=YuZ#-U=bk
zK_jb>auF&F^Ow|%+I;^&5rhF&l(Joqh}WRO_4atg2F)H*;X5VVY+k>+F!6~V1lr$<
zoYaF-H!uJ_EJ5gDwP5Pt5V4 at CbI=a|@d4m==|{FSfp!J~S^!T0YPMZ~8fdGwm$eOm
z00=21P6HYNwR;-8&}+PQ{sp|yYwJG%L}gy{KKQT?Sn%O%P14ob>gnrlJ}MmHkMQR@
zeQaI5;I3)6TI>0?HcPFgrsi1tQNh|O)bZ|COHFN^(9XBF31`~^*81l9+UmaU^W7i%
ayQ{l8yZZvw=eqk^&w|M#U0wO-h<^e3E-~x?
diff --git a/flang/test/Driver/Inputs/libfun.f90 b/flang/test/Driver/Inputs/libfun.f90
new file mode 100644
index 0000000000000..1df7b61e8bc85
--- /dev/null
+++ b/flang/test/Driver/Inputs/libfun.f90
@@ -0,0 +1,4 @@
+subroutine libfun(x)
+ integer :: x
+end subroutine
+
diff --git a/flang/test/Driver/mlink-builtin-bc.f90 b/flang/test/Driver/mlink-builtin-bc.f90
index 0d655a24a7a63..319373279d8e1 100644
--- a/flang/test/Driver/mlink-builtin-bc.f90
+++ b/flang/test/Driver/mlink-builtin-bc.f90
@@ -1,15 +1,12 @@
-
-!----------
-! RUN lines
-!----------
-! Embed something that can be easily checked
-! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - -mlink-builtin-bitcode %S/Inputs/bclib.bc %s 2>&1 | FileCheck %s
+! Test -mlink-builtin-bitcode flag
+! RUN: %flang -emit-llvm -c -o %t.bc %S/Inputs/libfun.f90
+! RUN: %flang_fc1 -emit-llvm -o - -mlink-builtin-bitcode %t.bc %s 2>&1 | FileCheck %s
! CHECK: define internal void @libfun_
-! RUN1: not %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - -mlink-builtin-bitcode %S/Inputs/no-bclib.bc %s 2>&1 | FileCheck %s
+! RUN: not %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu -o - -mlink-builtin-bitcode %no-%t.bc %s 2>&1 | FileCheck %s --check-prefix=ERROR
-! ERROR1: error: could not open {{.*}} no-bclib.bc
+! ERROR: error: could not open {{.*}}.bc
external libfun
parameter(i=1)
More information about the cfe-commits
mailing list