diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6164,8 +6164,19 @@ Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs); - Args.AddLastArg(CmdArgs, options::OPT_femulated_tls, - options::OPT_fno_emulated_tls); + + if (Arg *A = Args.getLastArg(options::OPT_femulated_tls, + options::OPT_fno_emulated_tls)) { + // LLVM does not support Emulated TLS for some architectures, like RISCV + // So issue an error to avoid subtle problems in codegen if the flags are + // incompatible. see https://github.com/llvm/llvm-project/issues/59500 + if (!Triple.supportsEmulatedTLS() && + A->getOption().getID() == options::OPT_femulated_tls) + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; + A->render(Args, CmdArgs); + } + Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ); if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) { diff --git a/clang/test/Driver/emulated-tls.cpp b/clang/test/Driver/emulated-tls.cpp --- a/clang/test/Driver/emulated-tls.cpp +++ b/clang/test/Driver/emulated-tls.cpp @@ -29,6 +29,10 @@ // RUN: %clang -### -target i686-pc-openbsd %s -femulated-tls -fno-emulated-tls 2>&1 \ // RUN: | FileCheck -check-prefix=NOEMU %s +// RUN: %clang -### --target=riscv64-linux-android %s -fno-emulated-tls 2>&1 \ +// RUN: | FileCheck -check-prefix=RISCV %s +// RUN: %clang -### --target=riscv64-linux-android %s -fno-emulated-tls -femulated-tls 2>&1 \ +// RUN: | FileCheck -check-prefix=RISCV-ERROR %s // Default without -f[no-]emulated-tls, will be decided by the target triple. // DEFAULT-NOT: "-cc1" {{.*}}"-femulated-tls" @@ -40,3 +44,6 @@ // NOEMU: "-cc1" {{.*}}"-fno-emulated-tls" // NOEMU-NOT: "-cc1" {{.*}}"-femulated-tls" + +// RISCV-NOT: error: unsupported option '-fno-emulated-tls' for target 'riscv64-unknown-linux-android' +// RISCV-ERROR: error: unsupported option '-femulated-tls' for target 'riscv64-unknown-linux-android'