awarzynski created this revision.
awarzynski added reviewers: rovka, kiranchandramohan, Leporacanthicus,
unterumarmung, ekieri.
Herald added a subscriber: mgorny.
Herald added a reviewer: sscalpone.
Herald added projects: Flang, All.
awarzynski requested review of this revision.
Herald added subscribers: cfe-commits, jdoerfert, MaskRay.
Herald added a project: clang.
This change is mostly about making sure that Flang's driver recognises
LLVM IR and BC as supported as supported file formats. To this end,
`isFortran` is extended and renamed as `isSupportedByFlang` (the latter
better reflects the new functionality).
Some new tests are added to verify that the target triple is overridden
by the frontend driver's default or the value specified with `-triple`.
Strictly speaking, this is not a functionality that's added here.
However, this patch enables us to write such tests and hence I'm
including them here.
Depends on D124664 <https://reviews.llvm.org/D124664>
Depends on D124665 <https://reviews.llvm.org/D124665>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D124667
Files:
clang/include/clang/Driver/Types.h
clang/lib/Driver/Driver.cpp
clang/lib/Driver/Types.cpp
flang/lib/Frontend/CMakeLists.txt
flang/lib/Frontend/FrontendActions.cpp
flang/lib/Frontend/FrontendOptions.cpp
flang/test/Driver/emit-asm-from-llvm-bc.ll
flang/test/Driver/emit-asm-from-llvm.ll
flang/test/Driver/missing-triple.ll
flang/test/Driver/override-triple.ll
flang/test/lit.cfg.py
Index: flang/test/lit.cfg.py
===================================================================
--- flang/test/lit.cfg.py
+++ flang/test/lit.cfg.py
@@ -27,7 +27,8 @@
# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.c', '.cpp', '.f', '.F', '.ff', '.FOR', '.for', '.f77', '.f90', '.F90',
'.ff90', '.f95', '.F95', '.ff95', '.fpp', '.FPP', '.cuf'
- '.CUF', '.f18', '.F18', '.fir', '.f03', '.F03', '.f08', '.F08']
+ '.CUF', '.f18', '.F18', '.fir', '.f03', '.F03', '.f08',
+ '.F08', '.ll']
config.substitutions.append(('%PATH%', config.environment['PATH']))
config.substitutions.append(('%llvmshlibdir', config.llvm_shlib_dir))
Index: flang/test/Driver/override-triple.ll
===================================================================
--- /dev/null
+++ flang/test/Driver/override-triple.ll
@@ -0,0 +1,25 @@
+; Verify that the module triple is overridden by the driver - even in the presence
+; of a module triple.
+; NOTE: At the time of writing, the tested behaviour was consistent with Clang
+
+;-------------
+; RUN COMMANDS
+;-------------
+; RUN: %flang_fc1 -S %s -o - 2>&1 | FileCheck %s
+; RUN: %flang -S %s -o - 2>&1 | FileCheck %s
+
+;----------------
+; EXPECTED OUTPUT
+;----------------
+; CHECK: warning: overriding the module target triple with {{.*}}
+
+;------
+; INPUT
+;------
+; For the triple to be overridden by the driver, it needs to be different to the host triple.
+; Use a random string to guaranteed that.
+target triple = "invalid-triple"
+
+define void @foo() {
+ ret void
+}
Index: flang/test/Driver/missing-triple.ll
===================================================================
--- /dev/null
+++ flang/test/Driver/missing-triple.ll
@@ -0,0 +1,21 @@
+; Verify that the module triple is overridden by the driver - even when the
+; module triple is missing.
+; NOTE: At the time of writing, the tested behaviour was consistent with Clang
+
+;-------------
+; RUN COMMANDS
+;-------------
+; RUN: %flang_fc1 -S %s -o - 2>&1 | FileCheck %s
+; RUN: %flang -S %s -o - 2>&1 | FileCheck %s
+
+;----------------
+; EXPECTED OUTPUT
+;----------------
+; CHECK: warning: overriding the module target triple with {{.*}}
+
+;------
+; INPUT
+;------
+define void @foo() {
+ ret void
+}
Index: flang/test/Driver/emit-asm-from-llvm.ll
===================================================================
--- /dev/null
+++ flang/test/Driver/emit-asm-from-llvm.ll
@@ -0,0 +1,23 @@
+; Verify that the driver can consume LLVM IR files. The expected assembly is ;
+; fairly generic (verified on AArch64 an X86_64), but we may need to tweak when
+; testing on other platforms. Note that that the actual output doesn't matter
+; as long as it's in Assembly format.
+
+;-------------
+; RUN COMMANDS
+;-------------
+; RUN: %flang_fc1 -S %s -o - | FileCheck %s
+; RUN: %flang -S %s -o - | FileCheck %s
+
+;----------------
+; EXPECTED OUTPUT
+;----------------
+; CHECK-LABEL: foo:
+; CHECK: ret
+
+;------
+; INPUT
+;------
+define void @foo() {
+ ret void
+}
Index: flang/test/Driver/emit-asm-from-llvm-bc.ll
===================================================================
--- /dev/null
+++ flang/test/Driver/emit-asm-from-llvm-bc.ll
@@ -0,0 +1,30 @@
+; Verify that the driver can consume LLVM BC files. The expected assembly is
+; fairly (tested on AArch64 an X86_64), but we may need to tweak when testing
+; on other platforms. Note that that the actual output doesn't matter as long
+; as it's in Assembly format.
+
+;-------------
+; RUN COMMANDS
+;-------------
+; RUN: rm -f %t.bc
+; RUN: %flang_fc1 -emit-llvm-bc %s -o %t.bc
+; RUN: %flang_fc1 -S -o - %t.bc | FileCheck %s
+; RUN: rm -f %t.bc
+
+; RUN: rm -f %t.bc
+; RUN: %flang -c -emit-llvm %s -o %t.bc
+; RUN: %flang -S -o - %t.bc | FileCheck %s
+; RUN: rm -f %t.bc
+
+;----------------
+; EXPECTED OUTPUT
+;----------------
+; CHECK-LABEL: foo:
+; CHECK: ret
+
+;------
+; INPUT
+;------
+define void @foo() {
+ ret void
+}
Index: flang/lib/Frontend/FrontendOptions.cpp
===================================================================
--- flang/lib/Frontend/FrontendOptions.cpp
+++ flang/lib/Frontend/FrontendOptions.cpp
@@ -35,5 +35,9 @@
if (isFixedFormSuffix(extension) || isFreeFormSuffix(extension)) {
return Language::Fortran;
}
+
+ if (extension == "bc" || extension == "ll")
+ return Language::LLVM_IR;
+
return Language::Unknown;
}
Index: flang/lib/Frontend/FrontendActions.cpp
===================================================================
--- flang/lib/Frontend/FrontendActions.cpp
+++ flang/lib/Frontend/FrontendActions.cpp
@@ -73,6 +73,17 @@
}
bool CodeGenAction::BeginSourceFileAction() {
+ llvmCtx = std::make_unique<llvm::LLVMContext>();
+
+ // If the input is an LLVM file, just parse it and return.
+ if (this->currentInput().kind().GetLanguage() == Language::LLVM_IR) {
+ llvm::SMDiagnostic err;
+ llvmModule = llvm::parseIRFile(currentInput().file(), err, *llvmCtx);
+
+ return (nullptr != llvmModule);
+ }
+
+ // ... otherwise, generate an MLIR module from the input Fortran source
bool res = RunPrescan() && RunParse() && RunSemanticChecks();
if (!res)
return res;
@@ -448,7 +459,6 @@
// Translate to LLVM IR
llvm::Optional<llvm::StringRef> moduleName = mlirModule->getName();
- llvmCtx = std::make_unique<llvm::LLVMContext>();
llvmModule = mlir::translateModuleToLLVMIR(
*mlirModule, *llvmCtx, moduleName ? *moduleName : "FIRModule");
Index: flang/lib/Frontend/CMakeLists.txt
===================================================================
--- flang/lib/Frontend/CMakeLists.txt
+++ flang/lib/Frontend/CMakeLists.txt
@@ -40,6 +40,7 @@
LINK_COMPONENTS
Passes
Analysis
+ IRReader
Option
Support
Target
Index: clang/lib/Driver/Types.cpp
===================================================================
--- clang/lib/Driver/Types.cpp
+++ clang/lib/Driver/Types.cpp
@@ -272,13 +272,16 @@
}
}
-bool types::isFortran(ID Id) {
+bool types::isAcceptedByFlang(ID Id) {
switch (Id) {
default:
return false;
case TY_Fortran: case TY_PP_Fortran:
return true;
+ case TY_LLVM_IR:
+ case TY_LLVM_BC:
+ return true;
}
}
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -5917,11 +5917,12 @@
bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
// Say "no" if there is not exactly one input of a type flang understands.
if (JA.size() != 1 ||
- !types::isFortran((*JA.input_begin())->getType()))
+ !types::isAcceptedByFlang((*JA.input_begin())->getType()))
return false;
// And say "no" if this is not a kind of action flang understands.
- if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA))
+ if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA) &&
+ !isa<BackendJobAction>(JA))
return false;
return true;
Index: clang/include/clang/Driver/Types.h
===================================================================
--- clang/include/clang/Driver/Types.h
+++ clang/include/clang/Driver/Types.h
@@ -66,6 +66,9 @@
/// isAcceptedByClang - Can clang handle this input type.
bool isAcceptedByClang(ID Id);
+ /// isAcceptedByFlang - Can flang handle this input type.
+ bool isAcceptedByFlang(ID Id);
+
/// isDerivedFromC - Is the input derived from C.
///
/// That is, does the lexer follow the rules of
@@ -92,9 +95,6 @@
/// isOpenCL - Is this an "OpenCL" input.
bool isOpenCL(ID Id);
- /// isFortran - Is this a Fortran input.
- bool isFortran(ID Id);
-
/// isSrcFile - Is this a source file, i.e. something that still has to be
/// preprocessed. The logic behind this is the same that decides if the first
/// compilation phase is a preprocessing one.
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits