[llvm] [InferAddressSpaces] Fix constant replace to avoid modifying other functions (PR #70611)

Wenju He via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 30 00:41:48 PDT 2023


================
@@ -1184,7 +1186,18 @@ bool InferAddressSpacesImpl::rewriteWithNewAddressSpaces(
       if (C != Replace) {
         LLVM_DEBUG(dbgs() << "Inserting replacement const cast: " << Replace
                           << ": " << *Replace << '\n');
-        C->replaceAllUsesWith(Replace);
+        VMap[C] = Replace;
+        for (User *U : make_early_inc_range(C->users())) {
+          for (auto It = df_begin(U), E = df_end(U); It != E;) {
----------------
wenju-he wrote:

A constant could be used nested inside another constant, an example is 
https://github.com/llvm/llvm-project/blob/30ca16ec87206294f4ad0e9688c88f32421b343e/llvm/test/Transforms/InferAddressSpaces/AMDGPU/infer-getelementptr.ll#L27
We can't replace constant within another constant directly because it may create new constant and in turn change other functions. So I need df to find the user instruction within the function and then replace the user in the instruction.

https://github.com/llvm/llvm-project/pull/70611


More information about the llvm-commits mailing list