Skip to content

Commit 0c41fa6

Browse files
agozillonEthanLuisMcDonough
authored andcommitted
[OMPIRBuilder][OpenMP][LLVM] Modify and use ReplaceConstant utility in convertTarget (llvm#94541)
This PR seeks to expand/replace the Constant -> Instruction conversion that needs to occur inside of the OpenMP Target kernel generation to allow kernel argument replacement of uses within the kernel (cannot replace constant uses within constant expressions with non-constants). It does so by making use of the new-ish utility convertUsersOfConstantsToInstructions which is a much more expansive version of what the smaller "version" of the function I wrote does, effectively expanding uses of the input argument that are constant expressions into instructions so that we can replace with the appropriate kernel argument. Also alters convertUsersOfConstantsToInstructions to optionally restrict the replacement to a function and optionally leave dead constants alone, the latter is necessary when lowering from MLIR as we cannot be sure we can remove the constants at this stage, even if rewritten to instructions the ModuleTranslation may maintain links to the original constants and utilise them in further lowering steps (as when we're lowering the kernel, the module is still in the process of being lowered). This can result in unusual ICEs later. These dead constants can be tidied up later (and appear to be in subsequent lowering from checking with emit-llvm).
1 parent b404f2f commit 0c41fa6

File tree

4 files changed

+88
-36
lines changed

4 files changed

+88
-36
lines changed

llvm/include/llvm/IR/ReplaceConstant.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@ namespace llvm {
1818

1919
template <typename T> class ArrayRef;
2020
class Constant;
21+
class Function;
2122

2223
/// Replace constant expressions users of the given constants with
2324
/// instructions. Return whether anything was changed.
24-
bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts);
25+
///
26+
/// Passing RestrictToFunc will restrict the constant replacement
27+
/// to the passed in functions scope, as opposed to the replacements
28+
/// occurring at module scope.
29+
///
30+
/// RemoveDeadConstants by default will remove all dead constants as
31+
/// the final step of the function after replacement, when passed
32+
/// false it will skip this final step.
33+
bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts,
34+
Function *RestrictToFunc = nullptr,
35+
bool RemoveDeadConstants = true);
2536

2637
} // end namespace llvm
2738

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/IR/MDBuilder.h"
4141
#include "llvm/IR/Metadata.h"
4242
#include "llvm/IR/PassManager.h"
43+
#include "llvm/IR/ReplaceConstant.h"
4344
#include "llvm/IR/Value.h"
4445
#include "llvm/MC/TargetRegistry.h"
4546
#include "llvm/Support/CommandLine.h"
@@ -5098,27 +5099,6 @@ FunctionCallee OpenMPIRBuilder::createDispatchFiniFunction(unsigned IVSize,
50985099
return getOrCreateRuntimeFunction(M, Name);
50995100
}
51005101

5101-
static void replaceConstatExprUsesInFuncWithInstr(ConstantExpr *ConstExpr,
5102-
Function *Func) {
5103-
for (User *User : make_early_inc_range(ConstExpr->users())) {
5104-
if (auto *Instr = dyn_cast<Instruction>(User)) {
5105-
if (Instr->getFunction() == Func) {
5106-
Instruction *ConstInst = ConstExpr->getAsInstruction();
5107-
ConstInst->insertBefore(*Instr->getParent(), Instr->getIterator());
5108-
Instr->replaceUsesOfWith(ConstExpr, ConstInst);
5109-
}
5110-
}
5111-
}
5112-
}
5113-
5114-
static void replaceConstantValueUsesInFuncWithInstr(llvm::Value *Input,
5115-
Function *Func) {
5116-
for (User *User : make_early_inc_range(Input->users()))
5117-
if (auto *Const = dyn_cast<Constant>(User))
5118-
if (auto *ConstExpr = dyn_cast<ConstantExpr>(Const))
5119-
replaceConstatExprUsesInFuncWithInstr(ConstExpr, Func);
5120-
}
5121-
51225102
static Function *createOutlinedFunction(
51235103
OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, StringRef FuncName,
51245104
SmallVectorImpl<Value *> &Inputs,
@@ -5197,17 +5177,23 @@ static Function *createOutlinedFunction(
51975177

51985178
// Things like GEP's can come in the form of Constants. Constants and
51995179
// ConstantExpr's do not have access to the knowledge of what they're
5200-
// contained in, so we must dig a little to find an instruction so we can
5201-
// tell if they're used inside of the function we're outlining. We also
5202-
// replace the original constant expression with a new instruction
5180+
// contained in, so we must dig a little to find an instruction so we
5181+
// can tell if they're used inside of the function we're outlining. We
5182+
// also replace the original constant expression with a new instruction
52035183
// equivalent; an instruction as it allows easy modification in the
5204-
// following loop, as we can now know the constant (instruction) is owned by
5205-
// our target function and replaceUsesOfWith can now be invoked on it
5206-
// (cannot do this with constants it seems). A brand new one also allows us
5207-
// to be cautious as it is perhaps possible the old expression was used
5208-
// inside of the function but exists and is used externally (unlikely by the
5209-
// nature of a Constant, but still).
5210-
replaceConstantValueUsesInFuncWithInstr(Input, Func);
5184+
// following loop, as we can now know the constant (instruction) is
5185+
// owned by our target function and replaceUsesOfWith can now be invoked
5186+
// on it (cannot do this with constants it seems). A brand new one also
5187+
// allows us to be cautious as it is perhaps possible the old expression
5188+
// was used inside of the function but exists and is used externally
5189+
// (unlikely by the nature of a Constant, but still).
5190+
// NOTE: We cannot remove dead constants that have been rewritten to
5191+
// instructions at this stage, we run the risk of breaking later lowering
5192+
// by doing so as we could still be in the process of lowering the module
5193+
// from MLIR to LLVM-IR and the MLIR lowering may still require the original
5194+
// constants we have created rewritten versions of.
5195+
if (auto *Const = dyn_cast<Constant>(Input))
5196+
convertUsersOfConstantsToInstructions(Const, Func, false);
52115197

52125198
// Collect all the instructions
52135199
for (User *User : make_early_inc_range(Input->users()))

llvm/lib/IR/ReplaceConstant.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ static SmallVector<Instruction *, 4> expandUser(BasicBlock::iterator InsertPt,
4949
return NewInsts;
5050
}
5151

52-
bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts) {
52+
bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts,
53+
Function *RestrictToFunc,
54+
bool RemoveDeadConstants) {
5355
// Find all expandable direct users of Consts.
5456
SmallVector<Constant *> Stack;
5557
for (Constant *C : Consts)
@@ -74,7 +76,8 @@ bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts) {
7476
for (Constant *C : ExpandableUsers)
7577
for (User *U : C->users())
7678
if (auto *I = dyn_cast<Instruction>(U))
77-
InstructionWorklist.insert(I);
79+
if (!RestrictToFunc || I->getFunction() == RestrictToFunc)
80+
InstructionWorklist.insert(I);
7881

7982
// Replace those expandable operands with instructions
8083
bool Changed = false;
@@ -102,8 +105,9 @@ bool convertUsersOfConstantsToInstructions(ArrayRef<Constant *> Consts) {
102105
}
103106
}
104107

105-
for (Constant *C : Consts)
106-
C->removeDeadConstantUsers();
108+
if (RemoveDeadConstants)
109+
for (Constant *C : Consts)
110+
C->removeDeadConstantUsers();
107111

108112
return Changed;
109113
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
! Offloading test which maps a specific element of a
2+
! derived type to the device and then accesses the
3+
! element alongside an individual element of an array
4+
! that the derived type contains. In particular, this
5+
! test helps to check that we can replace the constants
6+
! within the kernel with instructions and then replace
7+
! these instructions with the kernel parameters.
8+
! REQUIRES: flang
9+
! UNSUPPORTED: nvptx64-nvidia-cuda
10+
! UNSUPPORTED: nvptx64-nvidia-cuda-LTO
11+
! UNSUPPORTED: aarch64-unknown-linux-gnu
12+
! UNSUPPORTED: aarch64-unknown-linux-gnu-LTO
13+
! UNSUPPORTED: x86_64-pc-linux-gnu
14+
! UNSUPPORTED: x86_64-pc-linux-gnu-LTO
15+
16+
! RUN: %libomptarget-compile-fortran-run-and-check-generic
17+
module test_0
18+
type dtype
19+
integer elements(20)
20+
integer value
21+
end type dtype
22+
23+
type (dtype) array_dtype(5)
24+
contains
25+
26+
subroutine assign()
27+
implicit none
28+
!$omp target map(tofrom: array_dtype(5))
29+
array_dtype(5)%elements(5) = 500
30+
!$omp end target
31+
end subroutine
32+
33+
subroutine add()
34+
implicit none
35+
36+
!$omp target map(tofrom: array_dtype(5))
37+
array_dtype(5)%elements(5) = array_dtype(5)%elements(5) + 500
38+
!$omp end target
39+
end subroutine
40+
end module test_0
41+
42+
program main
43+
use test_0
44+
45+
call assign()
46+
call add()
47+
48+
print *, array_dtype(5)%elements(5)
49+
end program
50+
51+
! CHECK: 1000

0 commit comments

Comments
 (0)