Skip to content

Commit 3feb8b4

Browse files
authored
[HIP][HIPSTDPAR] Re-work allocation interposition for hipstdpar (#138790)
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.
1 parent 5dd1421 commit 3feb8b4

File tree

5 files changed

+93
-55
lines changed

5 files changed

+93
-55
lines changed

clang/docs/HIPSupport.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Predefined Macros
518518
* - ``__HIPSTDPAR__``
519519
- Defined when Clang is compiling code in algorithm offload mode, enabled
520520
with the ``--hipstdpar`` compiler option.
521-
* - ``__HIPSTDPAR_INTERPOSE_ALLOC__``
521+
* - ``__HIPSTDPAR_INTERPOSE_ALLOC__`` / ``__HIPSTDPAR_INTERPOSE_ALLOC_V1__``
522522
- Defined only when compiling in algorithm offload mode, when the user
523523
enables interposition mode with the ``--hipstdpar-interpose-alloc``
524524
compiler option, indicating that all dynamic memory allocation /

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,10 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
618618
Builder.defineMacro("__HIP_MEMORY_SCOPE_SYSTEM", "5");
619619
if (LangOpts.HIPStdPar) {
620620
Builder.defineMacro("__HIPSTDPAR__");
621-
if (LangOpts.HIPStdParInterposeAlloc)
621+
if (LangOpts.HIPStdParInterposeAlloc) {
622622
Builder.defineMacro("__HIPSTDPAR_INTERPOSE_ALLOC__");
623+
Builder.defineMacro("__HIPSTDPAR_INTERPOSE_ALLOC_V1__");
624+
}
623625
}
624626
if (LangOpts.CUDAIsDevice) {
625627
Builder.defineMacro("__HIP_DEVICE_COMPILE__");

clang/test/Preprocessor/predefined-macros.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,19 @@
304304
// RUN: %clang_cc1 %s -E -dM -o - -x hip --hipstdpar -triple x86_64-unknown-linux-gnu \
305305
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-HIPSTDPAR
306306
// CHECK-HIPSTDPAR: #define __HIPSTDPAR__ 1
307+
// CHECK-HIPSTDPAR-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC_V1__ 1
307308
// CHECK-HIPSTDPAR-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
308309

309310
// RUN: %clang_cc1 %s -E -dM -o - -x hip --hipstdpar --hipstdpar-interpose-alloc \
310311
// RUN: -triple x86_64-unknown-linux-gnu | FileCheck -match-full-lines %s \
311312
// RUN: --check-prefix=CHECK-HIPSTDPAR-INTERPOSE
313+
// CHECK-HIPSTDPAR-INTERPOSE: #define __HIPSTDPAR_INTERPOSE_ALLOC_V1__ 1
312314
// CHECK-HIPSTDPAR-INTERPOSE: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1
313315
// CHECK-HIPSTDPAR-INTERPOSE: #define __HIPSTDPAR__ 1
314316

315317
// RUN: %clang_cc1 %s -E -dM -o - -x hip --hipstdpar --hipstdpar-interpose-alloc \
316318
// RUN: -triple amdgcn-amd-amdhsa -fcuda-is-device | FileCheck -match-full-lines \
317319
// RUN: %s --check-prefix=CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG
318320
// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG: #define __HIPSTDPAR__ 1
321+
// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC_V1__ 1
319322
// CHECK-HIPSTDPAR-INTERPOSE-DEV-NEG-NOT: #define __HIPSTDPAR_INTERPOSE_ALLOC__ 1

llvm/lib/Transforms/HipStdPar/HipStdPar.cpp

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -231,45 +231,55 @@ PreservedAnalyses
231231
}
232232

233233
static constexpr std::pair<StringLiteral, StringLiteral> ReplaceMap[]{
234-
{"aligned_alloc", "__hipstdpar_aligned_alloc"},
235-
{"calloc", "__hipstdpar_calloc"},
236-
{"free", "__hipstdpar_free"},
237-
{"malloc", "__hipstdpar_malloc"},
238-
{"memalign", "__hipstdpar_aligned_alloc"},
239-
{"posix_memalign", "__hipstdpar_posix_aligned_alloc"},
240-
{"realloc", "__hipstdpar_realloc"},
241-
{"reallocarray", "__hipstdpar_realloc_array"},
242-
{"_ZdaPv", "__hipstdpar_operator_delete"},
243-
{"_ZdaPvm", "__hipstdpar_operator_delete_sized"},
244-
{"_ZdaPvSt11align_val_t", "__hipstdpar_operator_delete_aligned"},
245-
{"_ZdaPvmSt11align_val_t", "__hipstdpar_operator_delete_aligned_sized"},
246-
{"_ZdlPv", "__hipstdpar_operator_delete"},
247-
{"_ZdlPvm", "__hipstdpar_operator_delete_sized"},
248-
{"_ZdlPvSt11align_val_t", "__hipstdpar_operator_delete_aligned"},
249-
{"_ZdlPvmSt11align_val_t", "__hipstdpar_operator_delete_aligned_sized"},
250-
{"_Znam", "__hipstdpar_operator_new"},
251-
{"_ZnamRKSt9nothrow_t", "__hipstdpar_operator_new_nothrow"},
252-
{"_ZnamSt11align_val_t", "__hipstdpar_operator_new_aligned"},
253-
{"_ZnamSt11align_val_tRKSt9nothrow_t",
254-
"__hipstdpar_operator_new_aligned_nothrow"},
255-
256-
{"_Znwm", "__hipstdpar_operator_new"},
257-
{"_ZnwmRKSt9nothrow_t", "__hipstdpar_operator_new_nothrow"},
258-
{"_ZnwmSt11align_val_t", "__hipstdpar_operator_new_aligned"},
259-
{"_ZnwmSt11align_val_tRKSt9nothrow_t",
260-
"__hipstdpar_operator_new_aligned_nothrow"},
261-
{"__builtin_calloc", "__hipstdpar_calloc"},
262-
{"__builtin_free", "__hipstdpar_free"},
263-
{"__builtin_malloc", "__hipstdpar_malloc"},
264-
{"__builtin_operator_delete", "__hipstdpar_operator_delete"},
265-
{"__builtin_operator_new", "__hipstdpar_operator_new"},
266-
{"__builtin_realloc", "__hipstdpar_realloc"},
267-
{"__libc_calloc", "__hipstdpar_calloc"},
268-
{"__libc_free", "__hipstdpar_free"},
269-
{"__libc_malloc", "__hipstdpar_malloc"},
270-
{"__libc_memalign", "__hipstdpar_aligned_alloc"},
271-
{"__libc_realloc", "__hipstdpar_realloc"}
272-
};
234+
{"aligned_alloc", "__hipstdpar_aligned_alloc"},
235+
{"calloc", "__hipstdpar_calloc"},
236+
{"free", "__hipstdpar_free"},
237+
{"malloc", "__hipstdpar_malloc"},
238+
{"memalign", "__hipstdpar_aligned_alloc"},
239+
{"mmap", "__hipstdpar_mmap"},
240+
{"munmap", "__hipstdpar_munmap"},
241+
{"posix_memalign", "__hipstdpar_posix_aligned_alloc"},
242+
{"realloc", "__hipstdpar_realloc"},
243+
{"reallocarray", "__hipstdpar_realloc_array"},
244+
{"_ZdaPv", "__hipstdpar_operator_delete"},
245+
{"_ZdaPvm", "__hipstdpar_operator_delete_sized"},
246+
{"_ZdaPvSt11align_val_t", "__hipstdpar_operator_delete_aligned"},
247+
{"_ZdaPvmSt11align_val_t", "__hipstdpar_operator_delete_aligned_sized"},
248+
{"_ZdlPv", "__hipstdpar_operator_delete"},
249+
{"_ZdlPvm", "__hipstdpar_operator_delete_sized"},
250+
{"_ZdlPvSt11align_val_t", "__hipstdpar_operator_delete_aligned"},
251+
{"_ZdlPvmSt11align_val_t", "__hipstdpar_operator_delete_aligned_sized"},
252+
{"_Znam", "__hipstdpar_operator_new"},
253+
{"_ZnamRKSt9nothrow_t", "__hipstdpar_operator_new_nothrow"},
254+
{"_ZnamSt11align_val_t", "__hipstdpar_operator_new_aligned"},
255+
{"_ZnamSt11align_val_tRKSt9nothrow_t",
256+
"__hipstdpar_operator_new_aligned_nothrow"},
257+
258+
{"_Znwm", "__hipstdpar_operator_new"},
259+
{"_ZnwmRKSt9nothrow_t", "__hipstdpar_operator_new_nothrow"},
260+
{"_ZnwmSt11align_val_t", "__hipstdpar_operator_new_aligned"},
261+
{"_ZnwmSt11align_val_tRKSt9nothrow_t",
262+
"__hipstdpar_operator_new_aligned_nothrow"},
263+
{"__builtin_calloc", "__hipstdpar_calloc"},
264+
{"__builtin_free", "__hipstdpar_free"},
265+
{"__builtin_malloc", "__hipstdpar_malloc"},
266+
{"__builtin_operator_delete", "__hipstdpar_operator_delete"},
267+
{"__builtin_operator_new", "__hipstdpar_operator_new"},
268+
{"__builtin_realloc", "__hipstdpar_realloc"},
269+
{"__libc_calloc", "__hipstdpar_calloc"},
270+
{"__libc_free", "__hipstdpar_free"},
271+
{"__libc_malloc", "__hipstdpar_malloc"},
272+
{"__libc_memalign", "__hipstdpar_aligned_alloc"},
273+
{"__libc_realloc", "__hipstdpar_realloc"}};
274+
275+
static constexpr std::pair<StringLiteral, StringLiteral> HiddenMap[]{
276+
// hidden_malloc and hidden_free are only kept for backwards compatibility /
277+
// legacy purposes, and we should remove them in the future
278+
{"__hipstdpar_hidden_malloc", "__libc_malloc"},
279+
{"__hipstdpar_hidden_free", "__libc_free"},
280+
{"__hipstdpar_hidden_memalign", "__libc_memalign"},
281+
{"__hipstdpar_hidden_mmap", "mmap"},
282+
{"__hipstdpar_hidden_munmap", "munmap"}};
273283

274284
PreservedAnalyses
275285
HipStdParAllocationInterpositionPass::run(Module &M, ModuleAnalysisManager&) {
@@ -299,19 +309,14 @@ HipStdParAllocationInterpositionPass::run(Module &M, ModuleAnalysisManager&) {
299309
}
300310
}
301311

302-
if (auto F = M.getFunction("__hipstdpar_hidden_malloc")) {
303-
auto LibcMalloc = M.getOrInsertFunction(
304-
"__libc_malloc", F->getFunctionType(), F->getAttributes());
305-
F->replaceAllUsesWith(LibcMalloc.getCallee());
312+
for (auto &&HR : HiddenMap) {
313+
if (auto F = M.getFunction(HR.first)) {
314+
auto R = M.getOrInsertFunction(HR.second, F->getFunctionType(),
315+
F->getAttributes());
316+
F->replaceAllUsesWith(R.getCallee());
306317

307-
eraseFromModule(*F);
308-
}
309-
if (auto F = M.getFunction("__hipstdpar_hidden_free")) {
310-
auto LibcFree = M.getOrInsertFunction("__libc_free", F->getFunctionType(),
311-
F->getAttributes());
312-
F->replaceAllUsesWith(LibcFree.getCallee());
313-
314-
eraseFromModule(*F);
318+
eraseFromModule(*F);
319+
}
315320
}
316321

317322
return PreservedAnalyses::none();

llvm/test/Transforms/HipStdPar/allocation-interposition.ll

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ declare void @__hipstdpar_hidden_free(ptr)
1616

1717
declare ptr @__hipstdpar_hidden_malloc(i64)
1818

19+
declare ptr @__hipstdpar_hidden_memalign(i64, i64)
20+
21+
declare ptr @__hipstdpar_hidden_mmap(ptr, i64, i32, i32, i32, i64)
22+
23+
declare i32 @__hipstdpar_hidden_munmap(ptr, i64)
24+
25+
declare ptr @__hipstdpar_mmap(ptr, i64, i32, i32, i32, i64)
26+
27+
declare i32 @__hipstdpar_munmap(ptr, i64)
28+
1929
declare ptr @__hipstdpar_realloc(ptr, i64)
2030

2131
declare ptr @__hipstdpar_realloc_array(ptr, i64, i64)
@@ -171,7 +181,21 @@ define dso_local noundef i32 @allocs() {
171181
; CHECK: call void @__hipstdpar_free(ptr noundef %28)
172182
call void @__libc_free(ptr noundef %28)
173183

174-
ret i32 0
184+
; CHECK: %29 = call ptr @__libc_malloc(i64 noundef 8)
185+
%29 = call ptr @__hipstdpar_hidden_malloc(i64 noundef 8)
186+
; CHECK: call void @__libc_free(ptr noundef %29)
187+
call void @__hipstdpar_hidden_free(ptr noundef %29)
188+
189+
; CHECK: %30 = call ptr @__libc_memalign(i64 noundef 8, i64 noundef 4)
190+
%30 = call ptr @__hipstdpar_hidden_memalign(i64 noundef 8, i64 noundef 4)
191+
; CHECK: %31 = call ptr @mmap(ptr %30, i64 8, i32 0, i32 0, i32 0, i64 0)
192+
%31 = call ptr @__hipstdpar_hidden_mmap(ptr %30, i64 8, i32 0, i32 0, i32 0, i64 0)
193+
; CHECK: %32 = call i32 @munmap(ptr %31, i64 8)
194+
%32 = call i32 @__hipstdpar_hidden_munmap(ptr %31, i64 8)
195+
; CHECK: call void @__libc_free(ptr noundef %30)
196+
call void @__hipstdpar_hidden_free(ptr noundef %30)
197+
198+
ret i32 %32
175199
}
176200

177201
declare noalias ptr @aligned_alloc(i64 noundef, i64 noundef)
@@ -220,4 +244,8 @@ declare void @__libc_free(ptr noundef)
220244

221245
declare ptr @__libc_malloc(i64 noundef)
222246

223-
declare ptr @__libc_memalign(i64 noundef, i64 noundef)
247+
declare ptr @__libc_memalign(i64 noundef, i64 noundef)
248+
249+
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef)
250+
251+
declare i32 @munmap(ptr noundef, i64 noundef)

0 commit comments

Comments
 (0)