Index: clang/include/clang/Basic/TargetOptions.h =================================================================== --- clang/include/clang/Basic/TargetOptions.h +++ clang/include/clang/Basic/TargetOptions.h @@ -78,6 +78,18 @@ /// \brief If enabled, allow AMDGPU unsafe floating point atomics. bool AllowAMDGPUUnsafeFPAtomics = false; + /// \brief Enumeration value for AMDGPU code object version, which is the + /// code object version times 100. + enum CodeObjectVersionKind { + COV_None, + COV_2 = 200, + COV_3 = 300, + COV_4 = 400, + COV_5 = 500, + }; + /// \brief Code object version for AMDGPU. + CodeObjectVersionKind CodeObjectVersion; + // The code model to be used as specified by the user. Corresponds to // CodeModel::Model enum defined in include/llvm/Support/CodeGen.h, plus // "default" for the case when the user has not explicitly specified a Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -3442,8 +3442,12 @@ NegFlag>, Group; def mcode_object_version_EQ : Joined<["-"], "mcode-object-version=">, Group, - HelpText<"Specify code object ABI version. Defaults to 4. (AMDGPU only)">, - MetaVarName<"">, Values<"2,3,4,5">; + HelpText<"Specify code object ABI version. Allowed values are 2, 3, 4, and 5. Defaults to 4. (AMDGPU only)">, + Flags<[CC1Option]>, + Values<"none,2,3,4,5">, + NormalizedValuesScope<"TargetOptions">, + NormalizedValues<["COV_None", "COV_2", "COV_3", "COV_4", "COV_5"]>, + MarshallingInfoEnum, "COV_4">; defm code_object_v3_legacy : SimpleMFlag<"code-object-v3", "Legacy option to specify code object ABI V3", Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -552,21 +552,32 @@ EmitMainVoidAlias(); } - // Emit reference of __amdgpu_device_library_preserve_asan_functions to - // preserve ASAN functions in bitcode libraries. - if (LangOpts.Sanitize.has(SanitizerKind::Address) && getTriple().isAMDGPU()) { - auto *FT = llvm::FunctionType::get(VoidTy, {}); - auto *F = llvm::Function::Create( - FT, llvm::GlobalValue::ExternalLinkage, - "__amdgpu_device_library_preserve_asan_functions", &getModule()); - auto *Var = new llvm::GlobalVariable( - getModule(), FT->getPointerTo(), - /*isConstant=*/true, llvm::GlobalValue::WeakAnyLinkage, F, - "__amdgpu_device_library_preserve_asan_functions_ptr", nullptr, - llvm::GlobalVariable::NotThreadLocal); - addCompilerUsedGlobal(Var); - if (!getModule().getModuleFlag("amdgpu_hostcall")) { - getModule().addModuleFlag(llvm::Module::Override, "amdgpu_hostcall", 1); + if (getTriple().isAMDGPU()) { + // Emit reference of __amdgpu_device_library_preserve_asan_functions to + // preserve ASAN functions in bitcode libraries. + if (LangOpts.Sanitize.has(SanitizerKind::Address)) { + auto *FT = llvm::FunctionType::get(VoidTy, {}); + auto *F = llvm::Function::Create( + FT, llvm::GlobalValue::ExternalLinkage, + "__amdgpu_device_library_preserve_asan_functions", &getModule()); + auto *Var = new llvm::GlobalVariable( + getModule(), FT->getPointerTo(), + /*isConstant=*/true, llvm::GlobalValue::WeakAnyLinkage, F, + "__amdgpu_device_library_preserve_asan_functions_ptr", nullptr, + llvm::GlobalVariable::NotThreadLocal); + addCompilerUsedGlobal(Var); + if (!getModule().getModuleFlag("amdgpu_hostcall")) { + getModule().addModuleFlag(llvm::Module::Override, "amdgpu_hostcall", 1); + } + } + // Emit amdgpu_code_object_version module flag, which is code object version + // times 100. + // ToDo: Enable module flag for all code object version when ROCm device + // library is ready. + if (getTarget().getTargetOpts().CodeObjectVersion == TargetOptions::COV_5) { + getModule().addModuleFlag(llvm::Module::Error, + "amdgpu_code_object_version", + getTarget().getTargetOpts().CodeObjectVersion); } } Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -1150,7 +1150,8 @@ } static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, const ArgList &Args, - ArgStringList &CmdArgs) { + ArgStringList &CmdArgs, + bool IsCC1As = false) { // If no version was requested by the user, use the default value from the // back end. This is consistent with the value returned from // getAMDGPUCodeObjectVersion. This lets clang emit IR for amdgpu without @@ -1162,6 +1163,11 @@ Args.MakeArgString(Twine("--amdhsa-code-object-version=") + Twine(CodeObjVer))); CmdArgs.insert(CmdArgs.begin() + 1, "-mllvm"); + // -cc1as does not need -mcode-object-version option. + if (!IsCC1As) + CmdArgs.insert(CmdArgs.begin() + 1, + Args.MakeArgString(Twine("-mcode-object-version=") + + Twine(CodeObjVer))); } } @@ -7891,7 +7897,7 @@ } if (Triple.isAMDGPU()) - handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs); + handleAMDGPUCodeObjectVersionOptions(D, Args, CmdArgs, /*IsCC1As=*/true); assert(Input.isFilename() && "Invalid input."); CmdArgs.push_back(Input.getFilename()); Index: clang/test/CodeGenCUDA/amdgpu-asan-printf.cu =================================================================== --- clang/test/CodeGenCUDA/amdgpu-asan-printf.cu +++ clang/test/CodeGenCUDA/amdgpu-asan-printf.cu @@ -2,8 +2,7 @@ // RUN: -fcuda-is-device -target-cpu gfx906 -fsanitize=address \ // RUN: -O3 -x hip | FileCheck -check-prefixes=MFCHECK %s -// MFCHECK: !llvm.module.flags = !{![[FLAG1:[0-9]+]], ![[FLAG2:[0-9]+]]} -// MFCHECK: ![[FLAG1]] = !{i32 4, !"amdgpu_hostcall", i32 1} +// MFCHECK: !{{.*}} = !{i32 4, !"amdgpu_hostcall", i32 1} // Test to check hostcall module flag metadata is generated correctly // when a program has printf call and compiled with -fsanitize=address. Index: clang/test/CodeGenCUDA/amdgpu-asan.cu =================================================================== --- clang/test/CodeGenCUDA/amdgpu-asan.cu +++ clang/test/CodeGenCUDA/amdgpu-asan.cu @@ -27,8 +27,7 @@ // ASAN-DAG: @llvm.compiler.used = {{.*}}@__amdgpu_device_library_preserve_asan_functions_ptr // ASAN-DAG: define weak void @__asan_report_load1(i64 %{{.*}}) -// MFCHECK: !llvm.module.flags = !{![[FLAG1:[0-9]+]], ![[FLAG2:[0-9]+]]} -// MFCHECK: ![[FLAG1]] = !{i32 4, !"amdgpu_hostcall", i32 1} +// MFCHECK: !{{.*}} = !{i32 4, !"amdgpu_hostcall", i32 1} // CHECK-NOT: @__amdgpu_device_library_preserve_asan_functions // CHECK-NOT: @__asan_report_load1 Index: clang/test/CodeGenCUDA/amdgpu-code-object-version.cu =================================================================== --- /dev/null +++ clang/test/CodeGenCUDA/amdgpu-code-object-version.cu @@ -0,0 +1,30 @@ +// Create module flag for code object version. + +// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -o - %s | FileCheck %s -check-prefix=NONE + +// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -mcode-object-version=2 -o - %s | FileCheck -check-prefix=NONE %s + +// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -mcode-object-version=3 -o - %s | FileCheck -check-prefix=NONE %s + +// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -mcode-object-version=4 -o - %s | FileCheck -check-prefix=NONE %s + +// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -mcode-object-version=5 -o - %s | FileCheck -check-prefix=V5 %s + +// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -mcode-object-version=none -o - %s | FileCheck %s -check-prefix=NONE + +// RUN: not %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -emit-llvm \ +// RUN: -mcode-object-version=4.1 -o - %s 2>&1| FileCheck %s -check-prefix=INV + +// RUN: not %clang -cc1as -triple amdgcn-amd-amdhsa \ +// RUN: -mcode-object-version=5 -o - %s 2>&1 | FileCheck -check-prefix=AS %s + +// V5: !{{.*}} = !{i32 1, !"amdgpu_code_object_version", i32 500} +// NONE-NOT: !{{.*}} = !{i32 1, !"amdgpu_code_object_version", +// INV: error: invalid value '4.1' in '-mcode-object-version=4.1' +// AS: error: unknown argument: '-mcode-object-version=5' Index: clang/test/Driver/hip-code-object-version.hip =================================================================== --- clang/test/Driver/hip-code-object-version.hip +++ clang/test/Driver/hip-code-object-version.hip @@ -34,6 +34,7 @@ // RUN: %s 2>&1 | FileCheck -check-prefix=V3 %s // V3-WARN: warning: argument '-mcode-object-v3' is deprecated, use '-mcode-object-version=3' instead [-Wdeprecated] +// V3: "-mcode-object-version=3" // V3: "-mllvm" "--amdhsa-code-object-version=3" // V3: "-targets=host-x86_64-unknown-linux,hip-amdgcn-amd-amdhsa--gfx906" @@ -44,6 +45,7 @@ // RUN: --offload-arch=gfx906 -nogpulib \ // RUN: %s 2>&1 | FileCheck -check-prefix=V4 %s +// V4: "-mcode-object-version=4" // V4: "-mllvm" "--amdhsa-code-object-version=4" // V4: "-targets=host-x86_64-unknown-linux,hipv4-amdgcn-amd-amdhsa--gfx906" @@ -54,6 +56,7 @@ // RUN: --offload-arch=gfx906 -nogpulib \ // RUN: %s 2>&1 | FileCheck -check-prefix=V5 %s +// V5: "-mcode-object-version=5" // V5: "-mllvm" "--amdhsa-code-object-version=5" // V5: "-targets=host-x86_64-unknown-linux,hipv4-amdgcn-amd-amdhsa--gfx906"