diff --git a/llvm_passes/HipDynMem.cpp b/llvm_passes/HipDynMem.cpp index b4c0258f7..2fd4c9442 100644 --- a/llvm_passes/HipDynMem.cpp +++ b/llvm_passes/HipDynMem.cpp @@ -19,7 +19,6 @@ #include "HipDynMem.h" -#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SetVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/IR/Function.h" @@ -44,7 +43,6 @@ using namespace llvm; #define SPIR_LOCAL_AS 3 #define GENERIC_AS 4 -typedef llvm::SmallPtrSet FSet; typedef llvm::SetVector OrderedFSet; typedef llvm::SmallVector GVarVec; @@ -91,7 +89,7 @@ class HipDynMemExternReplacePass : public ModulePass { } } - static void recursivelyFindDirectUsers(Value *V, FSet &FS) { + static void recursivelyFindDirectUsers(Value *V, OrderedFSet &FS) { for (auto U : V->users()) { Instruction *Inst = dyn_cast(U); if (Inst) { @@ -440,7 +438,7 @@ CloneFunctionInto(NewF, F, VV, CloneFunctionChangeType::GlobalChanges, RI); for (GlobalVariable *GV : GVars) { - FSet DirectUserSet; + OrderedFSet DirectUserSet; // first, find functions that directly use the GVar. However, these may be // called from other functions, so we need to append the diff --git a/tests/compiler/CMakeLists.txt b/tests/compiler/CMakeLists.txt index 85c355d42..2d54704ee 100644 --- a/tests/compiler/CMakeLists.txt +++ b/tests/compiler/CMakeLists.txt @@ -202,6 +202,8 @@ add_hipcc_test(TestDeviceVirtualFunctions.hip HIPCC_OPTIONS -c) add_shell_test(TestDeadVTableVirtualBase.bash) # Program-scope variable init kernels must use their work items (#582). add_shell_test(TestFix582VarInitWorkItems.bash) +# Lowering a module with many shared memory users must be reproducible (#1661). +add_shell_test(TestFix1661DynMemOrder.bash) add_hipcc_test(TestHostSideHIPVectors.hip HIPCC_OPTIONS -fsyntax-only) add_hipcc_test(TestAlignAttr.hip HIPCC_OPTIONS -fsyntax-only) # Check CHIP_FAST_MATH is set for -ffast-math and preprocessor guards diff --git a/tests/compiler/TestFix1661DynMemOrder.bash b/tests/compiler/TestFix1661DynMemOrder.bash new file mode 100644 index 000000000..2d304836f --- /dev/null +++ b/tests/compiler/TestFix1661DynMemOrder.bash @@ -0,0 +1,31 @@ +#!/bin/bash +# Regression test for CHIP-SPV/chipStar#1661: lowering a module must not depend +# on allocation addresses, so repeated post-link pipeline runs agree. +set -eu + +OPT="@LLVM_TOOLS_BINARY_DIR@/opt" +PLUGIN="@CMAKE_BINARY_DIR@/lib/libLLVMHipSpvPasses.so" +OUT="@CMAKE_CURRENT_BINARY_DIR@/@TEST_NAME@.d" + +rm -rf "${OUT}"; mkdir -p "${OUT}"; cd "${OUT}" + +# More than 16 users: a small pointer set iterates in insertion order anyway. +{ + echo '@__smem = external addrspace(3) global [0 x i32]' + for i in $(seq 32); do + printf 'define spir_kernel void @k%d(i32 %%v) {\n' "${i}" + printf ' store i32 %%v, ptr addrspace(3) @__smem\n ret void\n}\n' + done +} > in.ll + +# Heap addresses only change between processes, so compare several runs. +for i in $(seq 8); do + "${OPT}" -load-pass-plugin "${PLUGIN}" -passes=hip-post-link-passes \ + in.ll -S -o "run${i}.ll" + if ! cmp -s run1.ll "run${i}.ll"; then + echo "FAIL: run ${i} lowered the same module differently from run 1" + diff run1.ll "run${i}.ll" | head -10 + exit 1 + fi +done +echo "PASSED"