[clang] [clang-repl] Change the default optimization for hip device code to O3 (PR #242296)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 05:05:11 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Aditya Sinha (AdityaSinha149)
<details>
<summary>Changes</summary>
This PR makes clang-repl compile HIP device code at `-O3` by default. A `-O` passed by the user still takes precedence.
Unoptimized HIP kernels request `hostcall` support, which needs `PCIe` atomics. On machines without them the kernel launch fails, so a simple kernel typed into clang-repl does not run.
Assisted by Claude Opus 5.5
---
Full diff: https://github.com/llvm/llvm-project/pull/242296.diff
3 Files Affected:
- (modified) clang/lib/Interpreter/DeviceOffload.cpp (-1)
- (modified) clang/lib/Interpreter/Interpreter.cpp (+6-1)
- (modified) clang/unittests/Interpreter/DeviceOffloadTest.cpp (+20)
``````````diff
diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp
index d7ae386466f40..44cf800a92646 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -64,7 +64,6 @@ IncrementalHIPDeviceParser::GenerateDeviceObject() {
auto &PTU = PTUs.back();
CodeGenOptions CodeGenOptsForObj = DeviceCI.getCodeGenOpts();
- CodeGenOptsForObj.DisableLLVMPasses = true;
ObjectContent.clear();
auto ObjOS = std::make_unique<llvm::raw_svector_ostream>(ObjectContent);
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index d6cce74b7275f..e5317ad666453 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -395,7 +395,7 @@ IncrementalCompilerBuilder::createOffload(bool device, OffloadType Type) {
const bool HipEnabled = Type == OffloadType::HIP;
std::vector<const char *> Argv;
- Argv.reserve(5 + 4 + UserArgs.size());
+ Argv.reserve(5 + 5 + UserArgs.size());
Argv.push_back(HipEnabled ? "-xhip" : "-xcuda");
Argv.push_back(device ? "--cuda-device-only" : "--cuda-host-only");
@@ -417,6 +417,11 @@ IncrementalCompilerBuilder::createOffload(bool device, OffloadType Type) {
std::string CUIDArg = "-cuid=" + OffloadCUID;
Argv.push_back(CUIDArg.c_str());
+ // Unoptimized HIP kernels request hostcall support, which needs PCIe atomics
+ // and fails to dispatch without them. A -O in UserArgs comes later and wins.
+ if (device && HipEnabled)
+ Argv.push_back("-O3");
+
llvm::append_range(Argv, UserArgs);
std::string TT = TargetTriple ? *TargetTriple : llvm::sys::getProcessTriple();
diff --git a/clang/unittests/Interpreter/DeviceOffloadTest.cpp b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
index fa624f23f435b..bfa2c31577d2a 100644
--- a/clang/unittests/Interpreter/DeviceOffloadTest.cpp
+++ b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
@@ -143,6 +143,26 @@ TEST(DeviceOffloadBuilderTest, MultipleOffloadArchsAreRejected) {
"supported. Pass a single --offload-arch"));
}
+TEST(DeviceOffloadBuilderTest, HIPDeviceDefaultsToO3) {
+ IncrementalCompilerBuilder CB;
+ CB.SetCompilerArgs({"--offload-arch=gfx1100", "-nogpulib", "-nogpuinc"});
+ auto DeviceCI = CB.CreateDevice(OffloadType::HIP);
+ ASSERT_THAT_EXPECTED(DeviceCI, llvm::Succeeded());
+ EXPECT_EQ((*DeviceCI)->getCodeGenOpts().OptimizationLevel, 3u);
+ auto HostCI = CB.CreateHost(OffloadType::HIP);
+ ASSERT_THAT_EXPECTED(HostCI, llvm::Succeeded());
+ EXPECT_EQ((*HostCI)->getCodeGenOpts().OptimizationLevel, 0u);
+}
+
+TEST(DeviceOffloadBuilderTest, UserOptLevelOverridesHIPDeviceDefault) {
+ IncrementalCompilerBuilder CB;
+ CB.SetCompilerArgs(
+ {"--offload-arch=gfx1100", "-nogpulib", "-nogpuinc", "-O1"});
+ auto DeviceCI = CB.CreateDevice(OffloadType::HIP);
+ ASSERT_THAT_EXPECTED(DeviceCI, llvm::Succeeded());
+ EXPECT_EQ((*DeviceCI)->getCodeGenOpts().OptimizationLevel, 1u);
+}
+
TEST_F(DeviceOffloadHIPTest, DeviceObjectIsGenerated) {
IncrementalCompilerBuilder CB;
CB.SetCompilerArgs({"--offload-arch=gfx1100", "-nogpulib", "-nogpuinc"});
``````````
</details>
https://github.com/llvm/llvm-project/pull/242296
More information about the cfe-commits
mailing list