Skip to content

Commit 6242c8c

Browse files
committed
[flang] add TBAA tags to global and direct variables
These turn out to be useful for spec2017/fotonik3d and safe so long as they are not used along side TBAA tags for local allocations. LLVM may be able to figure out local allocations by itself anyway. PR #68727
1 parent dddd0c2 commit 6242c8c

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

flang/include/flang/Optimizer/Analysis/TBAAForest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct TBAATree {
5555
SubtreeState globalDataTree;
5656
SubtreeState allocatedDataTree;
5757
SubtreeState dummyArgDataTree;
58+
SubtreeState directDataTree;
5859
mlir::LLVM::TBAATypeDescriptorAttr anyAccessDesc;
5960
mlir::LLVM::TBAATypeDescriptorAttr boxMemberTypeDesc;
6061
mlir::LLVM::TBAATypeDescriptorAttr anyDataTypeDesc;

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v) {
406406
attributes.set(Attribute::Pointer);
407407
}
408408

409-
if (type == SourceKind::Global)
409+
if (type == SourceKind::Global || type == SourceKind::Direct)
410410
return {global, type, ty, attributes, approximateSource};
411411

412412
return {v, type, ty, attributes, approximateSource};

flang/lib/Optimizer/Analysis/TBAAForest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ fir::TBAATree::TBAATree(mlir::LLVM::TBAATypeDescriptorAttr anyAccess,
5656
: globalDataTree(dataRoot.getContext(), "global data", dataRoot),
5757
allocatedDataTree(dataRoot.getContext(), "allocated data", dataRoot),
5858
dummyArgDataTree(dataRoot.getContext(), "dummy arg data", dataRoot),
59+
directDataTree(dataRoot.getContext(), "direct data", dataRoot),
5960
anyAccessDesc(anyAccess), boxMemberTypeDesc(boxMemberTypeDesc),
6061
anyDataTypeDesc(dataRoot) {}

flang/lib/Optimizer/Transforms/AddAliasTags.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ namespace fir {
3535
static llvm::cl::opt<bool>
3636
enableDummyArgs("dummy-arg-tbaa", llvm::cl::init(true), llvm::cl::Hidden,
3737
llvm::cl::desc("Add TBAA tags to dummy arguments"));
38-
// These two are **known unsafe** (misscompare in spec2017/wrf_r). They should
38+
static llvm::cl::opt<bool>
39+
enableGlobals("globals-tbaa", llvm::cl::init(true), llvm::cl::Hidden,
40+
llvm::cl::desc("Add TBAA tags to global variables"));
41+
static llvm::cl::opt<bool>
42+
enableDirect("direct-tbaa", llvm::cl::init(true), llvm::cl::Hidden,
43+
llvm::cl::desc("Add TBAA tags to direct variables"));
44+
// This is **known unsafe** (misscompare in spec2017/wrf_r). It should
3945
// not be enabled by default.
4046
// The code is kept so that these may be tried with new benchmarks to see if
4147
// this is worth fixing in the future.
42-
static llvm::cl::opt<bool>
43-
enableGlobals("globals-tbaa", llvm::cl::init(false), llvm::cl::Hidden,
44-
llvm::cl::desc("Add TBAA tags to global variables. UNSAFE."));
4548
static llvm::cl::opt<bool> enableLocalAllocs(
4649
"local-alloc-tbaa", llvm::cl::init(false), llvm::cl::Hidden,
4750
llvm::cl::desc("Add TBAA tags to local allocations. UNSAFE."));
@@ -158,6 +161,22 @@ void AddAliasTagsPass::runOnAliasInterface(fir::FirAliasTagOpInterface op,
158161
<< " at " << *op << "\n");
159162
tag = state.getFuncTree(func).globalDataTree.getTag(name);
160163

164+
// TBAA for SourceKind::Direct
165+
} else if (enableDirect &&
166+
source.kind == fir::AliasAnalysis::SourceKind::Direct) {
167+
if (source.u.is<mlir::SymbolRefAttr>()) {
168+
mlir::SymbolRefAttr glbl = source.u.get<mlir::SymbolRefAttr>();
169+
const char *name = glbl.getRootReference().data();
170+
LLVM_DEBUG(llvm::dbgs().indent(2) << "Found reference to direct " << name
171+
<< " at " << *op << "\n");
172+
tag = state.getFuncTree(func).directDataTree.getTag(name);
173+
} else {
174+
// SourceKind::Direct is likely to be extended to cases which are not a
175+
// SymbolRefAttr in the future
176+
LLVM_DEBUG(llvm::dbgs().indent(2) << "Can't get name for direct "
177+
<< source << " at " << *op << "\n");
178+
}
179+
161180
// TBAA for local allocations
162181
} else if (enableLocalAllocs &&
163182
source.kind == fir::AliasAnalysis::SourceKind::Allocate) {

flang/test/Transforms/tbaa2.fir

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,31 @@
4747
// CHECK: #[[ROOT:.+]] = #llvm.tbaa_root<id = "Flang function root _QMmodPcallee">
4848
// CHECK: #[[ANY_ACCESS:.+]] = #llvm.tbaa_type_desc<id = "any access", members = {<#[[ROOT]], 0>}>
4949
// CHECK: #[[ANY_DATA:.+]] = #llvm.tbaa_type_desc<id = "any data access", members = {<#[[ANY_ACCESS]], 0>}>
50+
// CHECK: #[[ANY_GLBL:.+]] = #llvm.tbaa_type_desc<id = "global data", members = {<#[[ANY_DATA]], 0>}>
5051
// CHECK: #[[ANY_ARG:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data", members = {<#[[ANY_DATA]], 0>}>
52+
// CHECK: #[[ANY_DIRECT:.+]] = #llvm.tbaa_type_desc<id = "direct data", members = {<#[[ANY_DATA]], 0>}>
53+
// CHECK: #[[GLBL_ZSTART:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEzstart", members = {<#[[ANY_GLBL]], 0>}>
54+
// CHECK: #[[GLBL_ZSTOP:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEzstop", members = {<#[[ANY_GLBL]], 0>}>
55+
// CHECK: #[[GLBL_YSTART:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEystart", members = {<#[[ANY_GLBL]], 0>}>
56+
// CHECK: #[[GLBL_YSTOP:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEystop", members = {<#[[ANY_GLBL]], 0>}>
57+
// CHECK: #[[GLBL_XSTART:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodExstart", members = {<#[[ANY_GLBL]], 0>}>
5158
// CHECK: #[[ARG_LOW:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data/_QMmodFcalleeElow", members = {<#[[ANY_ARG]], 0>}>
59+
// CHECK: #[[DIRECT_A:.+]] = #llvm.tbaa_type_desc<id = "direct data/_QMmodEa", members = {<#[[ANY_DIRECT]], 0>}>
60+
// CHECK: #[[DIRECT_B:.+]] = #llvm.tbaa_type_desc<id = "direct data/_QMmodEb", members = {<#[[ANY_DIRECT]], 0>}>
5261
// CHECK: #[[ARG_Z:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data/_QMmodFcalleeEz", members = {<#[[ANY_ARG]], 0>}>
62+
// CHECK: #[[GLBL_DYINV:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEdyinv", members = {<#[[ANY_GLBL]], 0>}>
5363
// CHECK: #[[ARG_Y:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data/_QMmodFcalleeEy", members = {<#[[ANY_ARG]], 0>}>
64+
65+
// CHECK: #[[GLBL_ZSTART_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_ZSTART]], access_type = #[[GLBL_ZSTART]], offset = 0>
66+
// CHECK: #[[GLBL_ZSTOP_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_ZSTOP]], access_type = #[[GLBL_ZSTOP]], offset = 0>
67+
// CHECK: #[[GLBL_YSTART_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_YSTART]], access_type = #[[GLBL_YSTART]], offset = 0>
68+
// CHECK: #[[GLBL_YSTOP_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_YSTOP]], access_type = #[[GLBL_YSTOP]], offset = 0>
69+
// CHECK: #[[GLBL_XSTART_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_XSTART]], access_type = #[[GLBL_XSTART]], offset = 0>
5470
// CHECK: #[[ARG_LOW_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[ARG_LOW]], access_type = #[[ARG_LOW]], offset = 0>
71+
// CHECK: #[[DIRECT_A_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[DIRECT_A]], access_type = #[[DIRECT_A]], offset = 0>
72+
// CHECK: #[[DIRECT_B_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[DIRECT_B]], access_type = #[[DIRECT_B]], offset = 0>
5573
// CHECK: #[[ARG_Z_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[ARG_Z]], access_type = #[[ARG_Z]], offset = 0>
74+
// CHECK: #[[GLBL_DYINV_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_DYINV]], access_type = #[[GLBL_DYINV]], offset = 0>
5675
// CHECK: #[[ARG_Y_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[ARG_Y]], access_type = #[[ARG_Y]], offset = 0>
5776
5877
func.func @_QMmodPcallee(%arg0: !fir.box<!fir.array<?x?x?xf32>> {fir.bindc_name = "z"}, %arg1: !fir.box<!fir.array<?x?x?xf32>> {fir.bindc_name = "y"}, %arg2: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?x?xf32>>>> {fir.bindc_name = "low"}) {
@@ -246,28 +265,23 @@
246265
// CHECK: %[[VAL_37:.*]] = fir.rebox %[[VAL_36]] : (!fir.box<!fir.array<?x?x?xf32>>) -> !fir.box<!fir.array<?x?x?xf32>>
247266
// CHECK: %[[VAL_38:.*]] = fir.declare %[[VAL_0]] {fortran_attrs = #{{.*}}<intent_in>, uniq_name = "_QMmodFcalleeEz"} : (!fir.box<!fir.array<?x?x?xf32>>) -> !fir.box<!fir.array<?x?x?xf32>>
248267
// CHECK: %[[VAL_39:.*]] = fir.rebox %[[VAL_38]] : (!fir.box<!fir.array<?x?x?xf32>>) -> !fir.box<!fir.array<?x?x?xf32>>
249-
// TODO: read from global assumed to always alias
250-
// CHECK: %[[VAL_40:.*]] = fir.load %[[VAL_22]] : !fir.ref<i32>
268+
// CHECK: %[[VAL_40:.*]] = fir.load %[[VAL_22]] {tbaa = [#[[GLBL_ZSTART_TAG]]]} : !fir.ref<i32>
251269
// CHECK: %[[VAL_41:.*]] = arith.addi %[[VAL_40]], %[[VAL_6]] : i32
252270
// CHECK: %[[VAL_42:.*]] = fir.convert %[[VAL_41]] : (i32) -> index
253-
// TODO: read from global assumed to always alias
254-
// CHECK: %[[VAL_43:.*]] = fir.load %[[VAL_24]] : !fir.ref<i32>
271+
// CHECK: %[[VAL_43:.*]] = fir.load %[[VAL_24]] {tbaa = [#[[GLBL_ZSTOP_TAG]]]} : !fir.ref<i32>
255272
// CHECK: %[[VAL_44:.*]] = fir.convert %[[VAL_43]] : (i32) -> index
256273
// CHECK: %[[VAL_45:.*]] = fir.convert %[[VAL_42]] : (index) -> i32
257274
// CHECK: %[[VAL_46:.*]]:2 = fir.do_loop %[[VAL_47:.*]] = %[[VAL_42]] to %[[VAL_44]] step %[[VAL_5]] iter_args(%[[VAL_48:.*]] = %[[VAL_45]]) -> (index, i32) {
258275
// CHECK: fir.store %[[VAL_48]] to %[[VAL_34]] : !fir.ref<i32>
259-
// TODO: read from global assumed to always alias
260-
// CHECK: %[[VAL_49:.*]] = fir.load %[[VAL_18]] : !fir.ref<i32>
276+
// CHECK: %[[VAL_49:.*]] = fir.load %[[VAL_18]] {tbaa = [#[[GLBL_YSTART_TAG]]]} : !fir.ref<i32>
261277
// CHECK: %[[VAL_50:.*]] = arith.addi %[[VAL_49]], %[[VAL_6]] : i32
262278
// CHECK: %[[VAL_51:.*]] = fir.convert %[[VAL_50]] : (i32) -> index
263-
// TODO: read from global assumed to always alias
264-
// CHECK: %[[VAL_52:.*]] = fir.load %[[VAL_20]] : !fir.ref<i32>
279+
// CHECK: %[[VAL_52:.*]] = fir.load %[[VAL_20]] {tbaa = [#[[GLBL_YSTOP_TAG]]]} : !fir.ref<i32>
265280
// CHECK: %[[VAL_53:.*]] = fir.convert %[[VAL_52]] : (i32) -> index
266281
// CHECK: %[[VAL_54:.*]] = fir.convert %[[VAL_51]] : (index) -> i32
267282
// CHECK: %[[VAL_55:.*]]:2 = fir.do_loop %[[VAL_56:.*]] = %[[VAL_51]] to %[[VAL_53]] step %[[VAL_5]] iter_args(%[[VAL_57:.*]] = %[[VAL_54]]) -> (index, i32) {
268283
// CHECK: fir.store %[[VAL_57]] to %[[VAL_32]] : !fir.ref<i32>
269-
// TODO: read from global assumed to always alias
270-
// CHECK: %[[VAL_58:.*]] = fir.load %[[VAL_16]] : !fir.ref<i32>
284+
// CHECK: %[[VAL_58:.*]] = fir.load %[[VAL_16]] {tbaa = [#[[GLBL_XSTART_TAG]]]} : !fir.ref<i32>
271285
// CHECK: %[[VAL_59:.*]] = arith.addi %[[VAL_58]], %[[VAL_6]] : i32
272286
// CHECK: %[[VAL_60:.*]] = fir.convert %[[VAL_59]] : (i32) -> index
273287
// CHECK: %[[VAL_61:.*]] = fir.convert %[[VAL_60]] : (index) -> i32
@@ -302,8 +316,7 @@
302316
// CHECK: %[[VAL_83:.*]]:3 = fir.box_dims %[[VAL_79]], %[[VAL_4]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
303317
// CHECK: %[[VAL_84:.*]] = fir.shape_shift %[[VAL_83]]#0, %[[VAL_83]]#1 : (index, index) -> !fir.shapeshift<1>
304318
// CHECK: %[[VAL_85:.*]] = fir.array_coor %[[VAL_82]](%[[VAL_84]]) %[[VAL_81]] : (!fir.heap<!fir.array<?xf32>>, !fir.shapeshift<1>, i64) -> !fir.ref<f32>
305-
// load from global variable
306-
// CHECK: %[[VAL_86:.*]] = fir.load %[[VAL_85]] : !fir.ref<f32>
319+
// CHECK: %[[VAL_86:.*]] = fir.load %[[VAL_85]] {tbaa = [#[[DIRECT_A_TAG]]]} : !fir.ref<f32>
307320
// load from box
308321
// CHECK: %[[VAL_87:.*]] = fir.load %[[VAL_35]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?x?xf32>>>>
309322
// load from local allocation
@@ -326,8 +339,7 @@
326339
// CHECK: %[[VAL_102:.*]]:3 = fir.box_dims %[[VAL_100]], %[[VAL_4]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
327340
// CHECK: %[[VAL_103:.*]] = fir.shape_shift %[[VAL_102]]#0, %[[VAL_102]]#1 : (index, index) -> !fir.shapeshift<1>
328341
// CHECK: %[[VAL_104:.*]] = fir.array_coor %[[VAL_101]](%[[VAL_103]]) %[[VAL_81]] : (!fir.heap<!fir.array<?xf32>>, !fir.shapeshift<1>, i64) -> !fir.ref<f32>
329-
// load from global variable
330-
// CHECK: %[[VAL_105:.*]] = fir.load %[[VAL_104]] : !fir.ref<f32>
342+
// CHECK: %[[VAL_105:.*]] = fir.load %[[VAL_104]] {tbaa = [#[[DIRECT_B_TAG]]]} : !fir.ref<f32>
331343
// CHECK: %[[VAL_106:.*]] = fir.array_coor %[[VAL_39]] %[[VAL_89]], %[[VAL_81]], %[[VAL_91]] : (!fir.box<!fir.array<?x?x?xf32>>, i64, i64, i64) -> !fir.ref<f32>
332344
// CHECK: %[[VAL_107:.*]] = fir.load %[[VAL_106]] {tbaa = [#[[ARG_Z_TAG]]]} : !fir.ref<f32>
333345
// CHECK: %[[VAL_108:.*]] = arith.subi %[[VAL_80]], %[[VAL_6]] : i32
@@ -336,8 +348,7 @@
336348
// CHECK: %[[VAL_111:.*]] = fir.load %[[VAL_110]] {tbaa = [#[[ARG_Z_TAG]]]} : !fir.ref<f32>
337349
// CHECK: %[[VAL_112:.*]] = arith.subf %[[VAL_107]], %[[VAL_111]] fastmath<contract> : f32
338350
// CHECK: %[[VAL_113:.*]] = fir.no_reassoc %[[VAL_112]] : f32
339-
// load from global variable
340-
// CHECK: %[[VAL_114:.*]] = fir.load %[[VAL_14]] : !fir.ref<f32>
351+
// CHECK: %[[VAL_114:.*]] = fir.load %[[VAL_14]] {tbaa = [#[[GLBL_DYINV_TAG]]]} : !fir.ref<f32>
341352
// CHECK: %[[VAL_115:.*]] = arith.mulf %[[VAL_113]], %[[VAL_114]] fastmath<contract> : f32
342353
// CHECK: %[[VAL_116:.*]] = arith.subi %[[VAL_90]], %[[VAL_6]] : i32
343354
// CHECK: %[[VAL_117:.*]] = fir.convert %[[VAL_116]] : (i32) -> i64

0 commit comments

Comments
 (0)