Files
clang-p2996/flang/lib/Optimizer/CodeGen/TBAABuilder.cpp
Markus Böck 1dda134f85 [mlir][flang] Convert TBAA metadata to an attribute representation
The current representation of TBAA is the very last in-tree user of the `llvm.metadata` operation.
Using ops to model metadata has a few disadvantages:
* Building a graph has to be done through some weakly typed indirection mechanism such as `SymbolRefAttr`
* Creating the metadata has to be done through a builder within a metadata op.
* It is not multithreading safe as operation insertion into the same block is not thread-safe

This patch therefore converts TBAA metadata into an attribute representation, in a similar manner as it has been done for alias groups and access groups in previous patches.

This additionally has the large benefit of giving us more "correctness by construction" as it makes things like cycles in a TBAA graph, or references to an incorrectly typed metadata node impossible.

Differential Revision: https://reviews.llvm.org/D155444
2023-07-19 16:42:50 +02:00

121 lines
4.0 KiB
C++

//===-- TBAABuilder.cpp -- TBAA builder definitions -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
//
//===----------------------------------------------------------------------===//
#include "flang/Optimizer/CodeGen/TBAABuilder.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "flang-tbaa-builder"
using namespace mlir;
using namespace mlir::LLVM;
static llvm::cl::opt<bool> disableTBAA(
"disable-tbaa",
llvm::cl::desc("disable attaching TBAA tags to memory accessing operations "
"to override default Flang behavior"),
llvm::cl::init(false));
// tagAttachmentLimit is a debugging option that allows limiting
// the number of TBAA access tag attributes attached to operations.
// It is set to kTagAttachmentUnlimited by default denoting "no limit".
static constexpr unsigned kTagAttachmentUnlimited =
std::numeric_limits<unsigned>::max();
static llvm::cl::opt<unsigned>
tagAttachmentLimit("tbaa-attach-tag-max", llvm::cl::desc(""),
llvm::cl::init(kTagAttachmentUnlimited));
namespace fir {
TBAABuilder::TBAABuilder(MLIRContext *context, bool applyTBAA)
: enableTBAA(applyTBAA && !disableTBAA) {
if (!enableTBAA)
return;
// Root node.
flangTBAARoot =
TBAARootAttr::get(context, StringAttr::get(context, flangTBAARootId));
// Any access node.
anyAccessTypeDesc = TBAATypeDescriptorAttr::get(
context, anyAccessTypeDescId, TBAAMemberAttr::get(flangTBAARoot, 0));
// Any data access node.
anyDataAccessTypeDesc =
TBAATypeDescriptorAttr::get(context, anyDataAccessTypeDescId,
TBAAMemberAttr::get(anyAccessTypeDesc, 0));
// Box member access node.
boxMemberTypeDesc = TBAATypeDescriptorAttr::get(
context, boxMemberTypeDescId, TBAAMemberAttr::get(anyAccessTypeDesc, 0));
}
TBAATagAttr TBAABuilder::getAccessTag(TBAATypeDescriptorAttr baseTypeDesc,
TBAATypeDescriptorAttr accessTypeDesc,
int64_t offset) {
TBAATagAttr &tag = tagsMap[{baseTypeDesc, accessTypeDesc, offset}];
if (tag)
return tag;
// Initialize new tag.
tag = TBAATagAttr::get(baseTypeDesc, accessTypeDesc, offset);
return tag;
}
TBAATagAttr TBAABuilder::getAnyBoxAccessTag() {
return getAccessTag(boxMemberTypeDesc, boxMemberTypeDesc, /*offset=*/0);
}
TBAATagAttr TBAABuilder::getBoxAccessTag(Type baseFIRType, Type accessFIRType,
GEPOp gep) {
return getAnyBoxAccessTag();
}
TBAATagAttr TBAABuilder::getAnyDataAccessTag() {
return getAccessTag(anyDataAccessTypeDesc, anyDataAccessTypeDesc,
/*offset=*/0);
}
TBAATagAttr TBAABuilder::getDataAccessTag(Type baseFIRType, Type accessFIRType,
GEPOp gep) {
return getAnyDataAccessTag();
}
void TBAABuilder::attachTBAATag(AliasAnalysisOpInterface op, Type baseFIRType,
Type accessFIRType, GEPOp gep) {
if (!enableTBAA)
return;
++tagAttachmentCounter;
if (tagAttachmentLimit != kTagAttachmentUnlimited &&
tagAttachmentCounter > tagAttachmentLimit)
return;
LLVM_DEBUG(llvm::dbgs() << "Attaching TBAA tag #" << tagAttachmentCounter
<< "\n");
TBAATagAttr tbaaTagSym;
if (baseFIRType.isa<fir::BaseBoxType>())
tbaaTagSym = getBoxAccessTag(baseFIRType, accessFIRType, gep);
else
tbaaTagSym = getDataAccessTag(baseFIRType, accessFIRType, gep);
if (!tbaaTagSym)
return;
op.setTBAATags(ArrayAttr::get(op->getContext(), tbaaTagSym));
}
} // namespace fir