Skip to content

Commit 0c09c3a

Browse files
committed
[Flang][OpenMP][MLIR] Fix common block mapping for regular and declare target link
This PR attempts to fix common block mapping for regular mapping of these types as well as when they have been marked as "declare target link". This PR should allow correct mapping of both the members of a common block and the full common block via its block symbol. The main changes were some adjustments to the Fortran OpenMP lowering to HLFIR/FIR, the lowering of the LLVM+OpenMP dialect to LLVM-IR and adjustments to the way the we handle target kernel map argument rebinding inside of the OMPIRBuilder. For the Fortran OpenMP lowering were two changes, one to prevent the implicit capture of common block members when the common block symbol itself has been marked and the other creates intermediate member access inside of the target region to be used in-place of those external to the target region, this prevents external usages breaking the IsolatedFromAbove pact. In the latter case, there was an adjustment to the size calculation for types to better handle cases where we pass an array as the type of a map (as opposed to the bounds and the type of the element), which occurs in the case of common blocks. There is also some adjustment to how handleDeclareTargetMapVar handles renaming of declare target symbols in the module to the reference pointer, now it will only apply to those within the kernel that is currently being generated and we also perform a modification to replace constants with instructions as necessary as we cannot replace these with our reference pointer (non-constant and constants do not mix nicely). In the case of the OpenMPIRBuilder some changes were mde to defer global symbol rebinding to kernel arguments until all other arguments have been rebound. This makes sure we do not replace uses that may refer to the global (e.g. a GEP) but are themselves actually a separate argument that needs bound. Currently "declare target to" still needs some work, but this may be the case for all types in conjunction with "declare target to" at the moment.
1 parent e3ca558 commit 0c09c3a

File tree

12 files changed

+607
-36
lines changed

12 files changed

+607
-36
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,33 @@ static void genBodyOfTargetDataOp(
830830
genNestedEvaluations(converter, eval);
831831
}
832832

833+
// This generates intermediate common block member accesses within a region
834+
// and then rebinds the members symbol to the intermediate accessors we have
835+
// generated so that subsequent code generation will utilise these instead.
836+
//
837+
// When the scope changes, the bindings to the intermediate accessors should
838+
// be dropped in place of the original symbol bindings.
839+
//
840+
// This is for utilisation with TargetOp.
841+
static void genIntermediateCommonBlockAccessors(
842+
Fortran::lower::AbstractConverter &converter,
843+
const mlir::Location &currentLocation, mlir::Region &region,
844+
llvm::ArrayRef<const Fortran::semantics::Symbol *> mapSyms) {
845+
for (auto [argIndex, argSymbol] : llvm::enumerate(mapSyms)) {
846+
if (auto *details =
847+
argSymbol->detailsIf<Fortran::semantics::CommonBlockDetails>()) {
848+
for (auto obj : details->objects()) {
849+
auto targetCBMemberBind = Fortran::lower::genCommonBlockMember(
850+
converter, currentLocation, *obj, region.getArgument(argIndex));
851+
fir::ExtendedValue sexv = converter.getSymbolExtendedValue(*obj);
852+
fir::ExtendedValue targetCBExv =
853+
getExtendedValue(sexv, targetCBMemberBind);
854+
converter.bindSymbol(*obj, targetCBExv);
855+
}
856+
}
857+
}
858+
}
859+
833860
// This functions creates a block for the body of the targetOp's region. It adds
834861
// all the symbols present in mapSymbols as block arguments to this block.
835862
static void
@@ -983,6 +1010,18 @@ genBodyOfTargetOp(Fortran::lower::AbstractConverter &converter,
9831010

9841011
// Create the insertion point after the marker.
9851012
firOpBuilder.setInsertionPointAfter(undefMarker.getDefiningOp());
1013+
1014+
// If we map a common block using it's symbol e.g. map(tofrom: /common_block/)
1015+
// and accessing it's members within the target region, there is a large
1016+
// chance we will end up with uses external to the region accessing the common
1017+
// block. As target regions are IsolatedFromAbove, we must make sure to
1018+
// resolve these, we do so by generating new common block member accesses
1019+
// within the region, binding them to the member symbol for the scope of the
1020+
// region so that subsequent code generation within the region will utilise
1021+
// our new member accesses we have created.
1022+
genIntermediateCommonBlockAccessors(converter, currentLocation, region,
1023+
mapSyms);
1024+
9861025
if (genNested)
9871026
genNestedEvaluations(converter, eval);
9881027
}
@@ -1574,6 +1613,13 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
15741613
// symbols used inside the region that have not been explicitly mapped using
15751614
// the map clause.
15761615
auto captureImplicitMap = [&](const Fortran::semantics::Symbol &sym) {
1616+
// if the symbol is part of an already mapped common block, do not make a
1617+
// map for it.
1618+
if (const Fortran::semantics::Symbol *common =
1619+
Fortran::semantics::FindCommonBlockContaining(sym.GetUltimate()))
1620+
if (llvm::find(mapSyms, common) != mapSyms.end())
1621+
return;
1622+
15771623
if (llvm::find(mapSyms, &sym) == mapSyms.end()) {
15781624
mlir::Value baseOp = converter.getSymbolAddress(sym);
15791625
if (!baseOp)

flang/test/Integration/OpenMP/map-types-and-sizes.f90

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,31 @@ subroutine mapType_char
231231
!$omp end target
232232
end subroutine mapType_char
233233

234+
!CHECK: @.offload_sizes{{.*}} = private unnamed_addr constant [1 x i64] [i64 8]
235+
!CHECK: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 35]
236+
subroutine mapType_common_block
237+
implicit none
238+
common /var_common/ var1, var2
239+
integer :: var1, var2
240+
!$omp target map(tofrom: /var_common/)
241+
var1 = var1 + 20
242+
var2 = var2 + 30
243+
!$omp end target
244+
end subroutine mapType_common_block
245+
246+
!CHECK: @.offload_sizes{{.*}} = private unnamed_addr constant [2 x i64] [i64 4, i64 4]
247+
!CHECK: @.offload_maptypes{{.*}} = private unnamed_addr constant [2 x i64] [i64 35, i64 35]
248+
subroutine mapType_common_block_members
249+
implicit none
250+
common /var_common/ var1, var2
251+
integer :: var1, var2
252+
253+
!$omp target map(tofrom: var1, var2)
254+
var2 = var1
255+
!$omp end target
256+
end subroutine mapType_common_block_members
257+
258+
234259
!CHECK-LABEL: define {{.*}} @{{.*}}maptype_ptr_explicit_{{.*}}
235260
!CHECK: %[[ALLOCA:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8 }, i64 1, align 8
236261
!CHECK: %[[ALLOCA_GEP:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8 }, ptr %[[ALLOCA]], i32 1
@@ -346,3 +371,19 @@ end subroutine mapType_char
346371
!CHECK: store ptr %[[ALLOCA]], ptr %[[BASE_PTR_ARR]], align 8
347372
!CHECK: %[[OFFLOAD_PTR_ARR:.*]] = getelementptr inbounds [1 x ptr], ptr %.offload_ptrs, i32 0, i32 0
348373
!CHECK: store ptr %[[ARR_OFF]], ptr %[[OFFLOAD_PTR_ARR]], align 8
374+
375+
!CHECK-LABEL: define {{.*}} @{{.*}}maptype_common_block_{{.*}}
376+
!CHECK: %[[BASE_PTR_ARR:.*]] = getelementptr inbounds [1 x ptr], ptr %.offload_baseptrs, i32 0, i32 0
377+
!CHECK: store ptr @var_common_, ptr %[[BASE_PTR_ARR]], align 8
378+
!CHECK: %[[OFFLOAD_PTR_ARR:.*]] = getelementptr inbounds [1 x ptr], ptr %.offload_ptrs, i32 0, i32 0
379+
!CHECK: store ptr @var_common_, ptr %[[OFFLOAD_PTR_ARR]], align 8
380+
381+
!CHECK-LABEL: define {{.*}} @{{.*}}maptype_common_block_members_{{.*}}
382+
!CHECK: %[[BASE_PTR_ARR:.*]] = getelementptr inbounds [2 x ptr], ptr %.offload_baseptrs, i32 0, i32 0
383+
!CHECK: store ptr @var_common_, ptr %[[BASE_PTR_ARR]], align 8
384+
!CHECK: %[[OFFLOAD_PTR_ARR:.*]] = getelementptr inbounds [2 x ptr], ptr %.offload_ptrs, i32 0, i32 0
385+
!CHECK: store ptr @var_common_, ptr %[[OFFLOAD_PTR_ARR]], align 8
386+
!CHECK: %[[BASE_PTR_ARR_1:.*]] = getelementptr inbounds [2 x ptr], ptr %.offload_baseptrs, i32 0, i32 1
387+
!CHECK: store ptr getelementptr (i8, ptr @var_common_, i64 4), ptr %[[BASE_PTR_ARR_1]], align 8
388+
!CHECK: %[[OFFLOAD_PTR_ARR_1:.*]] = getelementptr inbounds [2 x ptr], ptr %.offload_ptrs, i32 0, i32 1
389+
!CHECK: store ptr getelementptr (i8, ptr @var_common_, i64 4), ptr %[[OFFLOAD_PTR_ARR_1]], align 8
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
2+
3+
!CHECK: fir.global common @var_common_(dense<0> : vector<8xi8>) : !fir.array<8xi8>
4+
!CHECK: fir.global common @var_common_link_(dense<0> : vector<8xi8>) {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link)>} : !fir.array<8xi8>
5+
6+
!CHECK-LABEL: func.func @_QPmap_full_block
7+
!CHECK: %[[CB_ADDR:.*]] = fir.address_of(@var_common_) : !fir.ref<!fir.array<8xi8>>
8+
!CHECK: %[[MAP:.*]] = omp.map.info var_ptr(%[[CB_ADDR]] : !fir.ref<!fir.array<8xi8>>, !fir.array<8xi8>) map_clauses(tofrom) capture(ByRef) -> !fir.ref<!fir.array<8xi8>> {name = "var_common"}
9+
!CHECK: omp.target map_entries(%[[MAP]] -> %[[MAP_ARG:.*]] : !fir.ref<!fir.array<8xi8>>) {
10+
!CHECK: ^bb0(%[[MAP_ARG]]: !fir.ref<!fir.array<8xi8>>):
11+
!CHECK: %[[CONV:.*]] = fir.convert %[[MAP_ARG]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>
12+
!CHECK: %[[INDEX:.*]] = arith.constant 0 : index
13+
!CHECK: %[[COORD:.*]] = fir.coordinate_of %[[CONV]], %[[INDEX]] : (!fir.ref<!fir.array<?xi8>>, index) -> !fir.ref<i8>
14+
!CHECK: %[[CONV2:.*]] = fir.convert %[[COORD]] : (!fir.ref<i8>) -> !fir.ref<i32>
15+
!CHECK: %[[CB_MEMBER_1:.*]]:2 = hlfir.declare %[[CONV2]] {uniq_name = "_QFmap_full_blockEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
16+
!CHECK: %[[CONV3:.*]] = fir.convert %[[MAP_ARG]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>
17+
!CHECK: %[[INDEX2:.*]] = arith.constant 4 : index
18+
!CHECK: %[[COORD2:.*]] = fir.coordinate_of %[[CONV3]], %[[INDEX2]] : (!fir.ref<!fir.array<?xi8>>, index) -> !fir.ref<i8>
19+
!CHECK: %[[CONV4:.*]] = fir.convert %[[COORD2]] : (!fir.ref<i8>) -> !fir.ref<i32>
20+
!CHECK: %[[CB_MEMBER_2:.*]]:2 = hlfir.declare %[[CONV4]] {uniq_name = "_QFmap_full_blockEvar2"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
21+
subroutine map_full_block
22+
implicit none
23+
common /var_common/ var1, var2
24+
integer :: var1, var2
25+
!$omp target map(tofrom: /var_common/)
26+
var1 = var1 + 20
27+
var2 = var2 + 30
28+
!$omp end target
29+
end
30+
31+
!CHECK-LABEL: @_QPmap_mix_of_members
32+
!CHECK: %[[COMMON_BLOCK:.*]] = fir.address_of(@var_common_) : !fir.ref<!fir.array<8xi8>>
33+
!CHECK: %[[CB_CONV:.*]] = fir.convert %[[COMMON_BLOCK]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>
34+
!CHECK: %[[INDEX:.*]] = arith.constant 0 : index
35+
!CHECK: %[[COORD:.*]] = fir.coordinate_of %[[CB_CONV]], %[[INDEX]] : (!fir.ref<!fir.array<?xi8>>, index) -> !fir.ref<i8>
36+
!CHECK: %[[CONV:.*]] = fir.convert %[[COORD]] : (!fir.ref<i8>) -> !fir.ref<i32>
37+
!CHECK: %[[CB_MEMBER_1:.*]]:2 = hlfir.declare %[[CONV]] {uniq_name = "_QFmap_mix_of_membersEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
38+
!CHECK: %[[CB_CONV:.*]] = fir.convert %[[COMMON_BLOCK]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>
39+
!CHECK: %[[INDEX:.*]] = arith.constant 4 : index
40+
!CHECK: %[[COORD:.*]] = fir.coordinate_of %[[CB_CONV]], %[[INDEX]] : (!fir.ref<!fir.array<?xi8>>, index) -> !fir.ref<i8>
41+
!CHECK: %[[CONV:.*]] = fir.convert %[[COORD]] : (!fir.ref<i8>) -> !fir.ref<i32>
42+
!CHECK: %[[CB_MEMBER_2:.*]]:2 = hlfir.declare %[[CONV]] {uniq_name = "_QFmap_mix_of_membersEvar2"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
43+
!CHECK: %[[MAP_EXP:.*]] = omp.map.info var_ptr(%[[CB_MEMBER_2]]#0 : !fir.ref<i32>, i32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<i32> {name = "var2"}
44+
!CHECK: %[[MAP_IMP:.*]] = omp.map.info var_ptr(%[[CB_MEMBER_1]]#1 : !fir.ref<i32>, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !fir.ref<i32> {name = "var1"}
45+
!CHECK: omp.target map_entries(%[[MAP_EXP]] -> %[[ARG_EXP:.*]], %[[MAP_IMP]] -> %[[ARG_IMP:.*]] : !fir.ref<i32>, !fir.ref<i32>) {
46+
!CHECK: ^bb0(%[[ARG_EXP]]: !fir.ref<i32>, %[[ARG_IMP]]: !fir.ref<i32>):
47+
!CHECK: %[[EXP_MEMBER:.*]]:2 = hlfir.declare %[[ARG_EXP]] {uniq_name = "_QFmap_mix_of_membersEvar2"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
48+
!CHECK: %[[IMP_MEMBER:.*]]:2 = hlfir.declare %[[ARG_IMP]] {uniq_name = "_QFmap_mix_of_membersEvar1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
49+
subroutine map_mix_of_members
50+
implicit none
51+
common /var_common/ var1, var2
52+
integer :: var1, var2
53+
54+
!$omp target map(tofrom: var2)
55+
var2 = var1
56+
!$omp end target
57+
end
58+
59+
!CHECK-LABEL: @_QQmain
60+
!CHECK: %[[DECL_TAR_CB:.*]] = fir.address_of(@var_common_link_) : !fir.ref<!fir.array<8xi8>>
61+
!CHECK: %[[MAP_DECL_TAR_CB:.*]] = omp.map.info var_ptr(%[[DECL_TAR_CB]] : !fir.ref<!fir.array<8xi8>>, !fir.array<8xi8>) map_clauses(tofrom) capture(ByRef) -> !fir.ref<!fir.array<8xi8>> {name = "var_common_link"}
62+
!CHECK: omp.target map_entries(%[[MAP_DECL_TAR_CB]] -> %[[MAP_DECL_TAR_ARG:.*]] : !fir.ref<!fir.array<8xi8>>) {
63+
!CHECK: ^bb0(%[[MAP_DECL_TAR_ARG]]: !fir.ref<!fir.array<8xi8>>):
64+
!CHECK: %[[CONV:.*]] = fir.convert %[[MAP_DECL_TAR_ARG]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>
65+
!CHECK: %[[INDEX:.*]] = arith.constant 0 : index
66+
!CHECK: %[[COORD:.*]] = fir.coordinate_of %[[CONV]], %[[INDEX]] : (!fir.ref<!fir.array<?xi8>>, index) -> !fir.ref<i8>
67+
!CHECK: %[[CONV:.*]] = fir.convert %[[COORD]] : (!fir.ref<i8>) -> !fir.ref<i32>
68+
!CHECK: %[[MEMBER_ONE:.*]]:2 = hlfir.declare %[[CONV]] {uniq_name = "_QFElink1"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
69+
!CHECK: %[[CONV:.*]] = fir.convert %[[MAP_DECL_TAR_ARG]] : (!fir.ref<!fir.array<8xi8>>) -> !fir.ref<!fir.array<?xi8>>
70+
!CHECK: %[[INDEX:.*]] = arith.constant 4 : index
71+
!CHECK: %[[COORD:.*]] = fir.coordinate_of %[[CONV]], %[[INDEX]] : (!fir.ref<!fir.array<?xi8>>, index) -> !fir.ref<i8>
72+
!CHECK: %[[CONV:.*]] = fir.convert %[[COORD]] : (!fir.ref<i8>) -> !fir.ref<i32>
73+
!CHECK: %[[MEMBER_TWO:.*]]:2 = hlfir.declare %[[CONV]] {uniq_name = "_QFElink2"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
74+
program main
75+
implicit none
76+
common /var_common_link/ link1, link2
77+
integer :: link1, link2
78+
!$omp declare target link(/var_common_link/)
79+
80+
!$omp target map(tofrom: /var_common_link/)
81+
link1 = link2 + 20
82+
!$omp end target
83+
end program

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,11 @@ class OpenMPIRBuilder {
21102110
int32_t UB);
21112111
///}
21122112

2113+
/// Replaces constant values with instruction equivelants where possible
2114+
/// inside of a function.
2115+
static void replaceConstantValueUsesInFuncWithInstr(llvm::Value *Input,
2116+
Function *Func);
2117+
21132118
private:
21142119
// Sets the function attributes expected for the outlined function
21152120
void setOutlinedTargetRegionFunctionAttributes(Function *OutlinedFn);

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5085,8 +5085,8 @@ static void replaceConstatExprUsesInFuncWithInstr(ConstantExpr *ConstExpr,
50855085
}
50865086
}
50875087

5088-
static void replaceConstantValueUsesInFuncWithInstr(llvm::Value *Input,
5089-
Function *Func) {
5088+
void OpenMPIRBuilder::replaceConstantValueUsesInFuncWithInstr(
5089+
llvm::Value *Input, Function *Func) {
50905090
for (User *User : make_early_inc_range(Input->users()))
50915091
if (auto *Const = dyn_cast<Constant>(User))
50925092
if (auto *ConstExpr = dyn_cast<ConstantExpr>(Const))
@@ -5160,6 +5160,31 @@ static Function *createOutlinedFunction(
51605160
? make_range(Func->arg_begin() + 1, Func->arg_end())
51615161
: Func->args();
51625162

5163+
auto ReplaceValue = [&OMPBuilder](Value *Input, Value *InputCopy,
5164+
Function *Func) {
5165+
// Things like GEP's can come in the form of Constants. Constants and
5166+
// ConstantExpr's do not have access to the knowledge of what they're
5167+
// contained in, so we must dig a little to find an instruction so we
5168+
// can tell if they're used inside of the function we're outlining. We
5169+
// also replace the original constant expression with a new instruction
5170+
// equivalent; an instruction as it allows easy modification in the
5171+
// following loop, as we can now know the constant (instruction) is
5172+
// owned by our target function and replaceUsesOfWith can now be invoked
5173+
// on it (cannot do this with constants it seems). A brand new one also
5174+
// allows us to be cautious as it is perhaps possible the old expression
5175+
// was used inside of the function but exists and is used externally
5176+
// (unlikely by the nature of a Constant, but still).
5177+
OMPBuilder.replaceConstantValueUsesInFuncWithInstr(Input, Func);
5178+
5179+
// Collect all the instructions
5180+
for (User *User : make_early_inc_range(Input->users()))
5181+
if (auto *Instr = dyn_cast<Instruction>(User))
5182+
if (Instr->getFunction() == Func)
5183+
Instr->replaceUsesOfWith(Input, InputCopy);
5184+
};
5185+
5186+
SmallVector<std::pair<Value *, Value *>> DeferredReplacement;
5187+
51635188
// Rewrite uses of input valus to parameters.
51645189
for (auto InArg : zip(Inputs, ArgRange)) {
51655190
Value *Input = std::get<0>(InArg);
@@ -5169,27 +5194,36 @@ static Function *createOutlinedFunction(
51695194
Builder.restoreIP(
51705195
ArgAccessorFuncCB(Arg, Input, InputCopy, AllocaIP, Builder.saveIP()));
51715196

5172-
// Things like GEP's can come in the form of Constants. Constants and
5173-
// ConstantExpr's do not have access to the knowledge of what they're
5174-
// contained in, so we must dig a little to find an instruction so we can
5175-
// tell if they're used inside of the function we're outlining. We also
5176-
// replace the original constant expression with a new instruction
5177-
// equivalent; an instruction as it allows easy modification in the
5178-
// following loop, as we can now know the constant (instruction) is owned by
5179-
// our target function and replaceUsesOfWith can now be invoked on it
5180-
// (cannot do this with constants it seems). A brand new one also allows us
5181-
// to be cautious as it is perhaps possible the old expression was used
5182-
// inside of the function but exists and is used externally (unlikely by the
5183-
// nature of a Constant, but still).
5184-
replaceConstantValueUsesInFuncWithInstr(Input, Func);
5197+
// In certain cases a Global may be set up for replacement, however, this
5198+
// Global may be used in multiple arguments to the kernel, just segmented
5199+
// apart, for example, if we have a global array, that is sectioned into
5200+
// multiple mappings (technically not legal in OpenMP, but there is a case
5201+
// in Fortran for Common Blocks where this is neccesary), we will end up
5202+
// with GEP's into this array inside the kernel, that refer to the Global
5203+
// but are technically seperate arguments to the kernel for all intents and
5204+
// purposes. If we have mapped a segment that requires a GEP into the 0-th
5205+
// index, it will fold into an referal to the Global, if we then encounter
5206+
// this folded GEP during replacement all of the references to the
5207+
// Global in the kernel will be replaced with the argument we have generated
5208+
// that corresponds to it, including any other GEP's that refer to the
5209+
// Global that may be other arguments. This will invalidate all of the other
5210+
// preceding mapped arguments that refer to the same global that may be
5211+
// seperate segments. To prevent this, we defer global processing until all
5212+
// other processing has been performed.
5213+
if (llvm::isa<llvm::GlobalValue>(std::get<0>(InArg)) ||
5214+
llvm::isa<llvm::GlobalObject>(std::get<0>(InArg)) ||
5215+
llvm::isa<llvm::GlobalVariable>(std::get<0>(InArg))) {
5216+
DeferredReplacement.push_back(std::make_pair(Input, InputCopy));
5217+
continue;
5218+
}
51855219

5186-
// Collect all the instructions
5187-
for (User *User : make_early_inc_range(Input->users()))
5188-
if (auto *Instr = dyn_cast<Instruction>(User))
5189-
if (Instr->getFunction() == Func)
5190-
Instr->replaceUsesOfWith(Input, InputCopy);
5220+
ReplaceValue(Input, InputCopy, Func);
51915221
}
51925222

5223+
// Replace all of our deferred Input values, currently just Globals.
5224+
for (auto Deferred : DeferredReplacement)
5225+
ReplaceValue(std::get<0>(Deferred), std::get<1>(Deferred), Func);
5226+
51935227
// Restore insert point.
51945228
Builder.restoreIP(OldInsertPoint);
51955229

0 commit comments

Comments
 (0)