https://github.com/AdityaSinha149 updated 
https://github.com/llvm/llvm-project/pull/242296

>From b1357b30605426607b664a75e9faf0fe3b1655ad 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 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"});

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to