Skip to content

Commit 9ca1a15

Browse files
[flang][Driver] Add -masm option to flang (#81490)
The motivation here was a suggestion over in Compiler Explorer. You can use `-mllvm` already to do this but since gfortran supports `-masm`, I figured I'd try to add it. This is done by flang expanding `-masm` into `-mllvm x86-asm-syntax=`, then passing that to fc1. Which then collects all the `-mllvm` options and forwards them on. The code to expand it comes from clang `Clang::AddX86TargetArgs` (there are some other places doing the same thing too). However I've removed the `-inline-asm` that clang adds, as fortran doesn't have inline assembly. So `-masm` for flang purely changes the style of assembly output. ``` $ ./bin/flang-new /tmp/test.f90 -o - -S -target x86_64-linux-gnu <...> pushq %rbp $ ./bin/flang-new /tmp/test.f90 -o - -S -target x86_64-linux-gnu -masm=att <...> pushq %rbp $ ./bin/flang-new /tmp/test.f90 -o - -S -target x86_64-linux-gnu -masm=intel <...> push rbp ``` The test is adapted from `clang/test/Driver/masm.c` by removing the clang-cl related lines and changing the 32 bit triples to 64 bit triples since flang doesn't support 32 bit targets.
1 parent fe3406e commit 9ca1a15

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4414,7 +4414,7 @@ def mwatchsimulator_version_min_EQ : Joined<["-"], "mwatchsimulator-version-min=
44144414
def march_EQ : Joined<["-"], "march=">, Group<m_Group>,
44154415
Flags<[TargetSpecific]>, Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
44164416
HelpText<"For a list of available architectures for the target use '-mcpu=help'">;
4417-
def masm_EQ : Joined<["-"], "masm=">, Group<m_Group>;
4417+
def masm_EQ : Joined<["-"], "masm=">, Group<m_Group>, Visibility<[ClangOption, FlangOption]>;
44184418
def inline_asm_EQ : Joined<["-"], "inline-asm=">, Group<m_Group>,
44194419
Visibility<[ClangOption, CC1Option]>,
44204420
Values<"att,intel">,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ void Flang::AddRISCVTargetArgs(const ArgList &Args,
249249
}
250250
}
251251

252+
void Flang::AddX86_64TargetArgs(const ArgList &Args,
253+
ArgStringList &CmdArgs) const {
254+
if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
255+
StringRef Value = A->getValue();
256+
if (Value == "intel" || Value == "att") {
257+
CmdArgs.push_back(Args.MakeArgString("-mllvm"));
258+
CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
259+
} else {
260+
getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
261+
<< A->getSpelling() << Value;
262+
}
263+
}
264+
}
265+
252266
static void addVSDefines(const ToolChain &TC, const ArgList &Args,
253267
ArgStringList &CmdArgs) {
254268

@@ -374,6 +388,7 @@ void Flang::addTargetOptions(const ArgList &Args,
374388
break;
375389
case llvm::Triple::x86_64:
376390
getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
391+
AddX86_64TargetArgs(Args, CmdArgs);
377392
break;
378393
}
379394

clang/lib/Driver/ToolChains/Flang.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class LLVM_LIBRARY_VISIBILITY Flang : public Tool {
7777
void AddRISCVTargetArgs(const llvm::opt::ArgList &Args,
7878
llvm::opt::ArgStringList &CmdArgs) const;
7979

80+
/// Add specific options for X86_64 target.
81+
///
82+
/// \param [in] Args The list of input driver arguments
83+
/// \param [out] CmdArgs The list of output command arguments
84+
void AddX86_64TargetArgs(const llvm::opt::ArgList &Args,
85+
llvm::opt::ArgStringList &CmdArgs) const;
86+
8087
/// Extract offload options from the driver arguments and add them to
8188
/// the command arguments.
8289
/// \param [in] C The current compilation for the driver invocation

flang/test/Driver/masm.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
! RUN: %flang --target=x86_64-unknown-linux -masm=intel -S %s -### 2>&1 | FileCheck --check-prefix=CHECK-INTEL %s
2+
! RUN: %flang --target=x86_64-unknown-linux -masm=att -S %s -### 2>&1 | FileCheck --check-prefix=CHECK-ATT %s
3+
! RUN: not %flang --target=x86_64-unknown-linux -S -masm=somerequired %s -### 2>&1 | FileCheck --check-prefix=CHECK-SOMEREQUIRED %s
4+
! RUN: %flang --target=aarch64-unknown-eabi -S -masm=intel %s -### 2>&1 | FileCheck --check-prefix=CHECK-AARCH64 %s
5+
6+
! CHECK-INTEL: "-mllvm" "-x86-asm-syntax=intel"
7+
! CHECK-ATT: "-mllvm" "-x86-asm-syntax=att"
8+
! CHECK-SOMEREQUIRED: error: unsupported argument 'somerequired' to option '-masm='
9+
! CHECK-AARCH64: warning: argument unused during compilation: '-masm=intel'
10+
! CHECK-AARCH64-NOT: -x86-asm-syntax=intel

0 commit comments

Comments
 (0)