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 @@ -6166,6 +6166,17 @@ 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)) { + // LLVM does not support Emulated TLS for some architectures, like RISCV + // So issue an error to avoid subtle problems in codegen. + // https://github.com/llvm/llvm-project/issues/59500 + if (!Triple.supportsEmulatedTLS()) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getAsString(Args) << TripleStr; + } + } + 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 @@ -28,6 +28,9 @@ // RUN: | FileCheck -check-prefix=NOEMU %s // 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 -femulated-tls 2>&1 \ +// RUN: | FileCheck -check-prefix=RISCV-ERROR %s // Default without -f[no-]emulated-tls, will be decided by the target triple. @@ -40,3 +43,6 @@ // NOEMU: "-cc1" {{.*}}"-fno-emulated-tls" // NOEMU-NOT: "-cc1" {{.*}}"-femulated-tls" + +// RISCV-ERROR: unsupported option '-femulated-tls' for target 'riscv64-unknown-linux-android' +