https://github.com/AdityaSinha149 created https://github.com/llvm/llvm-project/pull/242296
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 >From c946e8fb9f49d4fabd7540f898a1d97a31abb538 Mon Sep 17 00:00:00 2001 From: AdityaSinha149 <[email protected]> Date: Fri, 13 Nov 2026 18:15:08 +0530 Subject: [PATCH] [clang-repl] Change the default optimization for hip device code to O3 --- clang/lib/Interpreter/DeviceOffload.cpp | 1 - clang/lib/Interpreter/Interpreter.cpp | 7 ++++++- .../Interpreter/DeviceOffloadTest.cpp | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp index 94fec23b1c0b2..010b40654ae74 100644 --- a/clang/lib/Interpreter/DeviceOffload.cpp +++ b/clang/lib/Interpreter/DeviceOffload.cpp @@ -63,7 +63,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 a195835f4fe8e..3b4fc72235771 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -327,7 +327,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"); @@ -349,6 +349,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 6cd052ac4701f..1d12193a40720 100644 --- a/clang/unittests/Interpreter/DeviceOffloadTest.cpp +++ b/clang/unittests/Interpreter/DeviceOffloadTest.cpp @@ -123,6 +123,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(DeviceOffloadHIPTest, DeviceObjectIsGenerated) { llvm::InitializeAllTargets(); llvm::InitializeAllTargetMCs(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
