Skip to content

Commit ba1bbbe

Browse files
committed
[flang] add TBAA tags to global 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.
1 parent 9168aa0 commit ba1bbbe

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

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/Transforms/AddAliasTags.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ 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+
// This is **known unsafe** (misscompare in spec2017/wrf_r). It should
3942
// not be enabled by default.
4043
// The code is kept so that these may be tried with new benchmarks to see if
4144
// 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."));
4545
static llvm::cl::opt<bool> enableLocalAllocs(
4646
"local-alloc-tbaa", llvm::cl::init(false), llvm::cl::Hidden,
4747
llvm::cl::desc("Add TBAA tags to local allocations. UNSAFE."));
@@ -151,7 +151,8 @@ void AddAliasTagsPass::runOnAliasInterface(fir::FirAliasTagOpInterface op,
151151

152152
// TBAA for global variables
153153
} else if (enableGlobals &&
154-
source.kind == fir::AliasAnalysis::SourceKind::Global) {
154+
(source.kind == fir::AliasAnalysis::SourceKind::Global ||
155+
source.kind == fir::AliasAnalysis::SourceKind::Direct)) {
155156
mlir::SymbolRefAttr glbl = source.u.get<mlir::SymbolRefAttr>();
156157
const char *name = glbl.getRootReference().data();
157158
LLVM_DEBUG(llvm::dbgs().indent(2) << "Found reference to global " << name

flang/test/Transforms/tbaa2.fir

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,30 @@
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: #[[GLBL_ZSTART:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEzstart", members = {<#[[ANY_GLBL]], 0>}>
53+
// CHECK: #[[GLBL_ZSTOP:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEzstop", members = {<#[[ANY_GLBL]], 0>}>
54+
// CHECK: #[[GLBL_YSTART:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEystart", members = {<#[[ANY_GLBL]], 0>}>
55+
// CHECK: #[[GLBL_YSTOP:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEystop", members = {<#[[ANY_GLBL]], 0>}>
56+
// CHECK: #[[GLBL_XSTART:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodExstart", members = {<#[[ANY_GLBL]], 0>}>
5157
// CHECK: #[[ARG_LOW:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data/_QMmodFcalleeElow", members = {<#[[ANY_ARG]], 0>}>
58+
// CHECK: #[[GLBL_A:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEa", members = {<#[[ANY_GLBL]], 0>}>
59+
// CHECK: #[[GLBL_B:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEb", members = {<#[[ANY_GLBL]], 0>}>
5260
// CHECK: #[[ARG_Z:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data/_QMmodFcalleeEz", members = {<#[[ANY_ARG]], 0>}>
61+
// CHECK: #[[GLBL_DYINV:.+]] = #llvm.tbaa_type_desc<id = "global data/_QMmodEdyinv", members = {<#[[ANY_GLBL]], 0>}>
5362
// CHECK: #[[ARG_Y:.+]] = #llvm.tbaa_type_desc<id = "dummy arg data/_QMmodFcalleeEy", members = {<#[[ANY_ARG]], 0>}>
63+
64+
// CHECK: #[[GLBL_ZSTART_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_ZSTART]], access_type = #[[GLBL_ZSTART]], offset = 0>
65+
// CHECK: #[[GLBL_ZSTOP_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_ZSTOP]], access_type = #[[GLBL_ZSTOP]], offset = 0>
66+
// CHECK: #[[GLBL_YSTART_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_YSTART]], access_type = #[[GLBL_YSTART]], offset = 0>
67+
// CHECK: #[[GLBL_YSTOP_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_YSTOP]], access_type = #[[GLBL_YSTOP]], offset = 0>
68+
// CHECK: #[[GLBL_XSTART_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_XSTART]], access_type = #[[GLBL_XSTART]], offset = 0>
5469
// CHECK: #[[ARG_LOW_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[ARG_LOW]], access_type = #[[ARG_LOW]], offset = 0>
70+
// CHECK: #[[GLBL_A_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_A]], access_type = #[[GLBL_A]], offset = 0>
71+
// CHECK: #[[GLBL_B_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_B]], access_type = #[[GLBL_B]], offset = 0>
5572
// CHECK: #[[ARG_Z_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[ARG_Z]], access_type = #[[ARG_Z]], offset = 0>
73+
// CHECK: #[[GLBL_DYINV_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[GLBL_DYINV]], access_type = #[[GLBL_DYINV]], offset = 0>
5674
// CHECK: #[[ARG_Y_TAG:.+]] = #llvm.tbaa_tag<base_type = #[[ARG_Y]], access_type = #[[ARG_Y]], offset = 0>
5775
5876
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 +264,23 @@
246264
// CHECK: %[[VAL_37:.*]] = fir.rebox %[[VAL_36]] : (!fir.box<!fir.array<?x?x?xf32>>) -> !fir.box<!fir.array<?x?x?xf32>>
247265
// 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>>
248266
// 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>
267+
// CHECK: %[[VAL_40:.*]] = fir.load %[[VAL_22]] {tbaa = [#[[GLBL_ZSTART_TAG]]]} : !fir.ref<i32>
251268
// CHECK: %[[VAL_41:.*]] = arith.addi %[[VAL_40]], %[[VAL_6]] : i32
252269
// 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>
270+
// CHECK: %[[VAL_43:.*]] = fir.load %[[VAL_24]] {tbaa = [#[[GLBL_ZSTOP_TAG]]]} : !fir.ref<i32>
255271
// CHECK: %[[VAL_44:.*]] = fir.convert %[[VAL_43]] : (i32) -> index
256272
// CHECK: %[[VAL_45:.*]] = fir.convert %[[VAL_42]] : (index) -> i32
257273
// 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) {
258274
// 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>
275+
// CHECK: %[[VAL_49:.*]] = fir.load %[[VAL_18]] {tbaa = [#[[GLBL_YSTART_TAG]]]} : !fir.ref<i32>
261276
// CHECK: %[[VAL_50:.*]] = arith.addi %[[VAL_49]], %[[VAL_6]] : i32
262277
// 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>
278+
// CHECK: %[[VAL_52:.*]] = fir.load %[[VAL_20]] {tbaa = [#[[GLBL_YSTOP_TAG]]]} : !fir.ref<i32>
265279
// CHECK: %[[VAL_53:.*]] = fir.convert %[[VAL_52]] : (i32) -> index
266280
// CHECK: %[[VAL_54:.*]] = fir.convert %[[VAL_51]] : (index) -> i32
267281
// 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) {
268282
// 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>
283+
// CHECK: %[[VAL_58:.*]] = fir.load %[[VAL_16]] {tbaa = [#[[GLBL_XSTART_TAG]]]} : !fir.ref<i32>
271284
// CHECK: %[[VAL_59:.*]] = arith.addi %[[VAL_58]], %[[VAL_6]] : i32
272285
// CHECK: %[[VAL_60:.*]] = fir.convert %[[VAL_59]] : (i32) -> index
273286
// CHECK: %[[VAL_61:.*]] = fir.convert %[[VAL_60]] : (index) -> i32
@@ -302,8 +315,7 @@
302315
// CHECK: %[[VAL_83:.*]]:3 = fir.box_dims %[[VAL_79]], %[[VAL_4]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
303316
// CHECK: %[[VAL_84:.*]] = fir.shape_shift %[[VAL_83]]#0, %[[VAL_83]]#1 : (index, index) -> !fir.shapeshift<1>
304317
// 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>
318+
// CHECK: %[[VAL_86:.*]] = fir.load %[[VAL_85]] {tbaa = [#[[GLBL_A_TAG]]]} : !fir.ref<f32>
307319
// load from box
308320
// CHECK: %[[VAL_87:.*]] = fir.load %[[VAL_35]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?x?xf32>>>>
309321
// load from local allocation
@@ -326,8 +338,7 @@
326338
// CHECK: %[[VAL_102:.*]]:3 = fir.box_dims %[[VAL_100]], %[[VAL_4]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
327339
// CHECK: %[[VAL_103:.*]] = fir.shape_shift %[[VAL_102]]#0, %[[VAL_102]]#1 : (index, index) -> !fir.shapeshift<1>
328340
// 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>
341+
// CHECK: %[[VAL_105:.*]] = fir.load %[[VAL_104]] {tbaa = [#[[GLBL_B_TAG]]]} : !fir.ref<f32>
331342
// 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>
332343
// CHECK: %[[VAL_107:.*]] = fir.load %[[VAL_106]] {tbaa = [#[[ARG_Z_TAG]]]} : !fir.ref<f32>
333344
// CHECK: %[[VAL_108:.*]] = arith.subi %[[VAL_80]], %[[VAL_6]] : i32
@@ -336,8 +347,7 @@
336347
// CHECK: %[[VAL_111:.*]] = fir.load %[[VAL_110]] {tbaa = [#[[ARG_Z_TAG]]]} : !fir.ref<f32>
337348
// CHECK: %[[VAL_112:.*]] = arith.subf %[[VAL_107]], %[[VAL_111]] fastmath<contract> : f32
338349
// 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>
350+
// CHECK: %[[VAL_114:.*]] = fir.load %[[VAL_14]] {tbaa = [#[[GLBL_DYINV_TAG]]]} : !fir.ref<f32>
341351
// CHECK: %[[VAL_115:.*]] = arith.mulf %[[VAL_113]], %[[VAL_114]] fastmath<contract> : f32
342352
// CHECK: %[[VAL_116:.*]] = arith.subi %[[VAL_90]], %[[VAL_6]] : i32
343353
// CHECK: %[[VAL_117:.*]] = fir.convert %[[VAL_116]] : (i32) -> i64

0 commit comments

Comments
 (0)