Skip to content

[HIP][HIPSTDPAR] Re-work allocation interposition for hipstdpar #138790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025

Conversation

AlexVlx
Copy link
Contributor

@AlexVlx AlexVlx commented May 7, 2025

The allocation interposition mode had a number of issues, which are primarily addressed in the library component via ROCm/rocThrust#543. However, it is necessary to interpose some additional symbols, which this patch does. Furthermore, to implement this in a compatible way, we guard the new implementation under a V1 macro, which is defined in addition to the existing __HIPSTDPAR_INTERPOSE_ALLOC__ one.

@llvmbot llvmbot added clang Clang issues not falling into any other category llvm:transforms labels May 7, 2025
@AlexVlx AlexVlx requested a review from yxsamliu May 7, 2025 00:59
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-llvm-transforms

Author: Alex Voicu (AlexVlx)

Changes

The allocation interposition mode had a number of issues, which are primarily addressed in the library component via <ROCm/rocThrust#543>. However, it is necessary to interpose some additional symbols, which this patch does. Furthermore, to implement this in a compatible way, we guard the new implementation under a V1 macro, which is defined in addition to the existing __HIPSTDPAR_INTERPOSE_ALLOC__ one.


Full diff: https://github.com/llvm/llvm-project/pull/138790.diff

5 Files Affected:

  • (modified) clang/docs/HIPSupport.rst (+1-1)
  • (modified) clang/lib/Frontend/InitPreprocessor.cpp (+3-1)
  • (modified) clang/test/Preprocessor/predefined-macros.c (+3)
  • (modified) llvm/lib/Transforms/HipStdPar/HipStdPar.cpp (+19-12)
  • (modified) llvm/test/Transforms/HipStdPar/allocation-interposition.ll (+30-2)
diff --git a/clang/docs/HIPSupport.rst b/clang/docs/HIPSupport.rst
index b2ac53843aeed..dfc27089f9d4d 100644
--- a/clang/docs/HIPSupport.rst
+++ b/clang/docs/HIPSupport.rst
@@ -518,7 +518,7 @@ Predefined Macros
    * - ``__HIPSTDPAR__``
      - Defined when Clang is compiling code in algorithm offload mode, enabled
        with the ``--hipstdpar`` compiler option.
-   * - ``__HIPSTDPAR_INTERPOSE_ALLOC__``
+   * - ``__HIPSTDPAR_INTERPOSE_ALLOC__`` / ``__HIPSTDPAR_INTERPOSE_ALLOC_V1__``
      - Defined only when compiling in algorithm offload mode, when the user
        enables interposition mode with the ``--hipstdpar-interpose-alloc``
        compiler option, indicating that all dynamic memory allocation /
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 906b0faf44067..96d6fb64a6319 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -618,8 +618,10 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
     Builder.defineMacro("__HIP_MEMORY_SCOPE_SYSTEM", "5");
     if (LangOpts.HIPStdPar) {
       Builder.defineMacro("__HIPSTDPAR__");
-      if (LangOpts.HIPStdParInterposeAlloc)
+      if (LangOpts.HIPStdParInterposeAlloc) {
         Builder.defineMacro("__HIPSTDPAR_INTERPOSE_ALLOC__");
+        Builder.defineMacro("__HIPSTDPAR_INTERPOSE_ALLOC_V1__");
+      }
     }
     if (LangOpts.CUDAIsDevice) {
       Builder.defineMacro("__HIP_DEVICE_COMPILE__");
diff --git a/clang/test/Preprocessor/predefined-macros.c b/clang/test/Preprocessor/predefined-macros.c
index 633ba4681ac52..b7765bfa2fb14 100644
--- a/clang/test/Preprocessor/predefined-macros.c
+++ b/clang/test/Preprocessor/predefined-macros.c
@@ -304,11 +304,13 @@
 // RUN: %clang_cc1 %s -E -dM -o - -x hip --hipstdpar -triple x86_64-unknown-linux-gnu \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-HIPSTDPAR
 // CHECK-HIPSTDPAR: #define __HIPSTDPAR__ 1
+// CHECK-HIPSTDPAR-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC_V1__ 1
 // CHECK-HIPSTDPAR-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
 
 // RUN: %clang_cc1 %s -E -dM -o - -x hip --hipstdpar --hipstdpar-interpose-alloc \
 // RUN:  -triple x86_64-unknown-linux-gnu | FileCheck -match-full-lines %s \
 // RUN:  --check-prefix=CHECK-HIPSTDPAR-INTERPOSE
+// CHECK-HIPSTDPAR-INTERPOSE: #define __HIPSTDPAR_INTERPOSE_ALLOC_V1__ 1
 // CHECK-HIPSTDPAR-INTERPOSE: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
 // CHECK-HIPSTDPAR-INTERPOSE: #define __HIPSTDPAR__ 1
 
@@ -316,4 +318,5 @@
 // RUN:  -triple amdgcn-amd-amdhsa -fcuda-is-device | FileCheck -match-full-lines \
 // RUN:  %s --check-prefix=CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG
 // CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG: #define __HIPSTDPAR__ 1
+// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC_V1__ 1
 // CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
diff --git a/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp b/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp
index 40164a34f08ac..03a65152e9833 100644
--- a/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp
+++ b/llvm/lib/Transforms/HipStdPar/HipStdPar.cpp
@@ -236,6 +236,8 @@ static constexpr std::pair<StringLiteral, StringLiteral> ReplaceMap[]{
   {"free",                      "__hipstdpar_free"},
   {"malloc",                    "__hipstdpar_malloc"},
   {"memalign",                  "__hipstdpar_aligned_alloc"},
+  {"mmap",                      "__hipstdpar_mmap"},
+  {"munmap",                    "__hipstdpar_munmap"},
   {"posix_memalign",            "__hipstdpar_posix_aligned_alloc"},
   {"realloc",                   "__hipstdpar_realloc"},
   {"reallocarray",              "__hipstdpar_realloc_array"},
@@ -271,6 +273,16 @@ static constexpr std::pair<StringLiteral, StringLiteral> ReplaceMap[]{
   {"__libc_realloc",            "__hipstdpar_realloc"}
 };
 
+static constexpr std::pair<StringLiteral, StringLiteral> HiddenMap[]{
+  // hidden_malloc and hidden_free are only kept for backwards compatibility /
+  // legacy purposes, and we should remove them in the future
+  {"__hipstdpar_hidden_malloc",   "__libc_malloc"},
+  {"__hipstdpar_hidden_free",     "__libc_free"},
+  {"__hipstdpar_hidden_memalign", "__libc_memalign"},
+  {"__hipstdpar_hidden_mmap",     "mmap"},
+  {"__hipstdpar_hidden_munmap",   "munmap"}
+};
+
 PreservedAnalyses
 HipStdParAllocationInterpositionPass::run(Module &M, ModuleAnalysisManager&) {
   SmallDenseMap<StringRef, StringRef> AllocReplacements(std::cbegin(ReplaceMap),
@@ -299,19 +311,14 @@ HipStdParAllocationInterpositionPass::run(Module &M, ModuleAnalysisManager&) {
     }
   }
 
-  if (auto F = M.getFunction("__hipstdpar_hidden_malloc")) {
-    auto LibcMalloc = M.getOrInsertFunction(
-        "__libc_malloc", F->getFunctionType(), F->getAttributes());
-    F->replaceAllUsesWith(LibcMalloc.getCallee());
+  for (auto &&HR : HiddenMap) {
+    if (auto F = M.getFunction(HR.first)) {
+      auto R = M.getOrInsertFunction(HR.second, F->getFunctionType(),
+                                     F->getAttributes());
+      F->replaceAllUsesWith(R.getCallee());
 
-    eraseFromModule(*F);
-  }
-  if (auto F = M.getFunction("__hipstdpar_hidden_free")) {
-    auto LibcFree = M.getOrInsertFunction("__libc_free", F->getFunctionType(),
-                                          F->getAttributes());
-    F->replaceAllUsesWith(LibcFree.getCallee());
-
-    eraseFromModule(*F);
+      eraseFromModule(*F);
+    }
   }
 
   return PreservedAnalyses::none();
diff --git a/llvm/test/Transforms/HipStdPar/allocation-interposition.ll b/llvm/test/Transforms/HipStdPar/allocation-interposition.ll
index 9ec284b1dedb7..bdc9951d0252c 100644
--- a/llvm/test/Transforms/HipStdPar/allocation-interposition.ll
+++ b/llvm/test/Transforms/HipStdPar/allocation-interposition.ll
@@ -16,6 +16,16 @@ declare void @__hipstdpar_hidden_free(ptr)
 
 declare ptr @__hipstdpar_hidden_malloc(i64)
 
+declare ptr @__hipstdpar_hidden_memalign(i64, i64)
+
+declare ptr @__hipstdpar_hidden_mmap(ptr, i64, i32, i32, i32, i64)
+
+declare i32 @__hipstdpar_hidden_munmap(ptr, i64)
+
+declare ptr @__hipstdpar_mmap(ptr, i64, i32, i32, i32, i64)
+
+declare i32 @__hipstdpar_munmap(ptr, i64)
+
 declare ptr @__hipstdpar_realloc(ptr, i64)
 
 declare ptr @__hipstdpar_realloc_array(ptr, i64, i64)
@@ -171,7 +181,21 @@ define dso_local noundef i32 @allocs() {
   ; CHECK: call void @__hipstdpar_free(ptr noundef %28)
   call void @__libc_free(ptr noundef %28)
 
-  ret i32 0
+  ; CHECK: %29 = call ptr @__libc_malloc(i64 noundef 8)
+  %29 = call ptr @__hipstdpar_hidden_malloc(i64 noundef 8)
+  ; CHECK: call void @__libc_free(ptr noundef %29)
+  call void @__hipstdpar_hidden_free(ptr noundef %29)
+
+  ; CHECK: %30 = call ptr @__libc_memalign(i64 noundef 8, i64 noundef 4)
+  %30 = call ptr @__hipstdpar_hidden_memalign(i64 noundef 8, i64 noundef 4)
+  ; CHECK: %31 = call ptr @mmap(ptr %30, i64 8, i32 0, i32 0, i32 0, i64 0)
+  %31 = call ptr @__hipstdpar_hidden_mmap(ptr %30, i64 8, i32 0, i32 0, i32 0, i64 0)
+  ; CHECK: %32 = call i32 @munmap(ptr %31, i64 8)
+  %32 = call i32 @__hipstdpar_hidden_munmap(ptr %31, i64 8)
+  ; CHECK: call void @__libc_free(ptr noundef %30)
+  call void @__hipstdpar_hidden_free(ptr noundef %30)
+
+  ret i32 %32
 }
 
 declare noalias ptr @aligned_alloc(i64 noundef, i64 noundef)
@@ -220,4 +244,8 @@ declare void @__libc_free(ptr noundef)
 
 declare ptr @__libc_malloc(i64 noundef)
 
-declare ptr @__libc_memalign(i64 noundef, i64 noundef)
\ No newline at end of file
+declare ptr @__libc_memalign(i64 noundef, i64 noundef)
+
+declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef)
+
+declare i32 @munmap(ptr noundef, i64 noundef)

Copy link

github-actions bot commented May 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@AlexVlx AlexVlx merged commit 3feb8b4 into llvm:main May 7, 2025
12 checks passed
@AlexVlx AlexVlx deleted the hipstdpar_interpose_alloc_v1 branch May 7, 2025 13:20
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 7, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building clang,llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/21807

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: lang/c/function_types/TestFunctionTypes.py (173 of 2891)
PASS: lldb-api :: functionalities/thread/concurrent_events/TestConcurrentBreakpointOneDelayBreakpointThreads.py (174 of 2891)
PASS: lldb-api :: functionalities/single-thread-step/TestSingleThreadStepTimeout.py (175 of 2891)
PASS: lldb-api :: functionalities/thread/concurrent_events/TestConcurrentManyBreakpoints.py (176 of 2891)
PASS: lldb-api :: functionalities/step-avoids-no-debug/TestStepNoDebug.py (177 of 2891)
PASS: lldb-api :: functionalities/step_scripted/TestStepScripted.py (178 of 2891)
PASS: lldb-api :: tools/lldb-dap/variables/children/TestDAP_variables_children.py (179 of 2891)
PASS: lldb-api :: lang/cpp/limit-debug-info/TestWithLimitDebugInfo.py (180 of 2891)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteHostInfo.py (181 of 2891)
UNRESOLVED: lldb-api :: tools/lldb-dap/optimized/TestDAP_optimized.py (182 of 2891)
******************** TEST 'lldb-api :: tools/lldb-dap/optimized/TestDAP_optimized.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/optimized -p TestDAP_optimized.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 3feb8b42e973f935883bc9e779645ecdae1a586d)
  clang revision 3feb8b42e973f935883bc9e779645ecdae1a586d
  llvm revision 3feb8b42e973f935883bc9e779645ecdae1a586d
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/optimized
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants