2014-03-04 22:59:00 +08:00
|
|
|
//===------ PollyIRBuilder.cpp --------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2014-03-04 22:59:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The Polly IRBuilder file contains Polly specific extensions for the IRBuilder
|
|
|
|
// that are used e.g. to emit the llvm.loop.parallel metadata.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/CodeGen/IRBuilder.h"
|
2014-10-02 23:31:24 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
#include "polly/Support/ScopHelper.h"
|
2017-08-23 05:58:48 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-03-04 22:59:00 +08:00
|
|
|
#include "llvm/IR/Metadata.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2017-04-03 15:42:50 +08:00
|
|
|
static const int MaxArraysInAliasScops = 10;
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Get a self referencing id metadata node.
|
2014-10-02 04:10:44 +08:00
|
|
|
///
|
2014-10-02 23:31:24 +08:00
|
|
|
/// The MDNode looks like this (if arg0/arg1 are not null):
|
2014-10-02 04:10:44 +08:00
|
|
|
///
|
2020-10-24 05:55:41 +08:00
|
|
|
/// '!n = distinct !{!n, arg0, arg1}'
|
2014-10-02 04:10:44 +08:00
|
|
|
///
|
2014-10-02 23:31:24 +08:00
|
|
|
/// @return The self referencing id metadata node.
|
2014-12-10 06:02:16 +08:00
|
|
|
static MDNode *getID(LLVMContext &Ctx, Metadata *arg0 = nullptr,
|
|
|
|
Metadata *arg1 = nullptr) {
|
2014-10-02 23:31:24 +08:00
|
|
|
MDNode *ID;
|
2014-12-10 06:02:16 +08:00
|
|
|
SmallVector<Metadata *, 3> Args;
|
2014-11-08 05:44:18 +08:00
|
|
|
// Reserve operand 0 for loop id self reference.
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2014-10-02 23:31:24 +08:00
|
|
|
|
|
|
|
if (arg0)
|
|
|
|
Args.push_back(arg0);
|
|
|
|
if (arg1)
|
|
|
|
Args.push_back(arg1);
|
|
|
|
|
2020-10-24 05:55:41 +08:00
|
|
|
ID = MDNode::getDistinct(Ctx, Args);
|
2014-10-02 23:31:24 +08:00
|
|
|
ID->replaceOperandWith(0, ID);
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:13:21 +08:00
|
|
|
ScopAnnotator::ScopAnnotator() : SE(nullptr), AliasScopeDomain(nullptr) {
|
|
|
|
// Push an empty staging BandAttr.
|
|
|
|
LoopAttrEnv.emplace_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopAnnotator::~ScopAnnotator() {
|
|
|
|
assert(LoopAttrEnv.size() == 1 && "Loop stack imbalance");
|
|
|
|
assert(!getStagingAttrEnv() && "Forgot to clear staging attr env");
|
|
|
|
}
|
2014-10-02 23:31:24 +08:00
|
|
|
|
2014-10-02 23:32:17 +08:00
|
|
|
void ScopAnnotator::buildAliasScopes(Scop &S) {
|
2014-10-02 23:31:24 +08:00
|
|
|
SE = S.getSE();
|
|
|
|
|
|
|
|
LLVMContext &Ctx = SE->getContext();
|
|
|
|
AliasScopeDomain = getID(Ctx, MDString::get(Ctx, "polly.alias.scope.domain"));
|
|
|
|
|
|
|
|
AliasScopeMap.clear();
|
|
|
|
OtherAliasScopeListMap.clear();
|
|
|
|
|
2017-08-23 05:58:48 +08:00
|
|
|
// We are only interested in arrays, but no scalar references. Scalars should
|
|
|
|
// be handled easily by basicaa.
|
|
|
|
SmallVector<ScopArrayInfo *, 10> Arrays;
|
|
|
|
for (ScopArrayInfo *Array : S.arrays())
|
|
|
|
if (Array->isArrayKind())
|
|
|
|
Arrays.push_back(Array);
|
|
|
|
|
2017-04-03 15:42:50 +08:00
|
|
|
// The construction of alias scopes is quadratic in the number of arrays
|
|
|
|
// involved. In case of too many arrays, skip the construction of alias
|
|
|
|
// information to avoid quadratic increases in compile time and code size.
|
2017-08-23 05:58:48 +08:00
|
|
|
if (Arrays.size() > MaxArraysInAliasScops)
|
2017-04-03 15:42:50 +08:00
|
|
|
return;
|
|
|
|
|
2014-10-02 23:31:24 +08:00
|
|
|
std::string AliasScopeStr = "polly.alias.scope.";
|
2017-08-23 05:58:48 +08:00
|
|
|
for (const ScopArrayInfo *Array : Arrays) {
|
2017-06-19 18:19:29 +08:00
|
|
|
assert(Array->getBasePtr() && "Base pointer must be present");
|
2017-02-09 17:34:42 +08:00
|
|
|
AliasScopeMap[Array->getBasePtr()] =
|
|
|
|
getID(Ctx, AliasScopeDomain,
|
|
|
|
MDString::get(Ctx, (AliasScopeStr + Array->getName()).c_str()));
|
2017-06-19 18:19:29 +08:00
|
|
|
}
|
2014-10-02 23:31:24 +08:00
|
|
|
|
2017-08-23 05:58:48 +08:00
|
|
|
for (const ScopArrayInfo *Array : Arrays) {
|
2014-10-02 23:31:24 +08:00
|
|
|
MDNode *AliasScopeList = MDNode::get(Ctx, {});
|
|
|
|
for (const auto &AliasScopePair : AliasScopeMap) {
|
2017-02-09 17:34:42 +08:00
|
|
|
if (Array->getBasePtr() == AliasScopePair.first)
|
2014-10-02 23:31:24 +08:00
|
|
|
continue;
|
|
|
|
|
2014-12-10 06:02:16 +08:00
|
|
|
Metadata *Args = {AliasScopePair.second};
|
2014-10-02 23:31:24 +08:00
|
|
|
AliasScopeList =
|
|
|
|
MDNode::concatenate(AliasScopeList, MDNode::get(Ctx, Args));
|
|
|
|
}
|
|
|
|
|
2017-02-09 17:34:42 +08:00
|
|
|
OtherAliasScopeListMap[Array->getBasePtr()] = AliasScopeList;
|
2014-10-02 23:31:24 +08:00
|
|
|
}
|
2014-03-04 22:59:00 +08:00
|
|
|
}
|
|
|
|
|
2014-10-02 23:32:17 +08:00
|
|
|
void ScopAnnotator::pushLoop(Loop *L, bool IsParallel) {
|
2014-10-02 04:10:44 +08:00
|
|
|
ActiveLoops.push_back(L);
|
|
|
|
|
2021-03-04 17:46:29 +08:00
|
|
|
if (IsParallel) {
|
|
|
|
LLVMContext &Ctx = SE->getContext();
|
|
|
|
MDNode *AccessGroup = MDNode::getDistinct(Ctx, {});
|
|
|
|
ParallelLoops.push_back(AccessGroup);
|
|
|
|
}
|
2021-03-16 01:13:21 +08:00
|
|
|
|
|
|
|
// Open an empty BandAttr context for loops nested in this one.
|
|
|
|
LoopAttrEnv.emplace_back();
|
2014-03-04 22:59:00 +08:00
|
|
|
}
|
|
|
|
|
2014-10-02 23:32:17 +08:00
|
|
|
void ScopAnnotator::popLoop(bool IsParallel) {
|
2014-10-02 04:10:44 +08:00
|
|
|
ActiveLoops.pop_back();
|
2014-03-04 22:59:00 +08:00
|
|
|
|
2021-03-04 17:46:29 +08:00
|
|
|
if (IsParallel) {
|
|
|
|
assert(!ParallelLoops.empty() && "Expected a parallel loop to pop");
|
|
|
|
ParallelLoops.pop_back();
|
|
|
|
}
|
2021-03-16 01:13:21 +08:00
|
|
|
|
|
|
|
// Exit the subloop context.
|
|
|
|
assert(!getStagingAttrEnv() && "Forgot to clear staging attr env");
|
|
|
|
assert(LoopAttrEnv.size() >= 2 && "Popped too many");
|
|
|
|
LoopAttrEnv.pop_back();
|
2014-03-04 22:59:00 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:38:46 +08:00
|
|
|
void ScopAnnotator::annotateLoopLatch(BranchInst *B, Loop *L, bool IsParallel,
|
|
|
|
bool IsLoopVectorizerDisabled) const {
|
2021-03-04 17:46:29 +08:00
|
|
|
LLVMContext &Ctx = SE->getContext();
|
|
|
|
SmallVector<Metadata *, 3> Args;
|
|
|
|
|
|
|
|
// For the LoopID self-reference.
|
|
|
|
Args.push_back(nullptr);
|
2017-08-23 01:38:46 +08:00
|
|
|
|
2021-03-16 01:13:21 +08:00
|
|
|
// Add the user-defined loop properties to the annotation, if any. Any
|
|
|
|
// additional properties are appended.
|
|
|
|
// FIXME: What to do if these conflict?
|
|
|
|
MDNode *MData = nullptr;
|
|
|
|
if (BandAttr *AttrEnv = getActiveAttrEnv()) {
|
|
|
|
MData = AttrEnv->Metadata;
|
|
|
|
if (MData)
|
|
|
|
llvm::append_range(Args, drop_begin(MData->operands(), 1));
|
|
|
|
}
|
|
|
|
|
2017-08-23 01:38:46 +08:00
|
|
|
if (IsLoopVectorizerDisabled) {
|
2021-03-04 17:46:29 +08:00
|
|
|
MDString *PropName = MDString::get(Ctx, "llvm.loop.vectorize.enable");
|
|
|
|
ConstantInt *FalseValue = ConstantInt::get(Type::getInt1Ty(Ctx), 0);
|
|
|
|
ValueAsMetadata *PropValue = ValueAsMetadata::get(FalseValue);
|
|
|
|
Args.push_back(MDNode::get(Ctx, {PropName, PropValue}));
|
2017-08-23 01:38:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsParallel) {
|
2021-03-04 17:46:29 +08:00
|
|
|
MDString *PropName = MDString::get(Ctx, "llvm.loop.parallel_accesses");
|
|
|
|
MDNode *AccGroup = ParallelLoops.back();
|
|
|
|
Args.push_back(MDNode::get(Ctx, {PropName, AccGroup}));
|
2017-08-23 01:38:46 +08:00
|
|
|
}
|
2014-03-04 22:59:00 +08:00
|
|
|
|
2021-03-04 17:46:29 +08:00
|
|
|
// No metadata to annotate.
|
2021-03-16 01:13:21 +08:00
|
|
|
if (!MData && Args.size() <= 1)
|
2021-03-04 17:46:29 +08:00
|
|
|
return;
|
|
|
|
|
2021-03-16 01:13:21 +08:00
|
|
|
// Reuse the MData node if possible, this will avoid having to create another
|
|
|
|
// one that cannot be merged because LoopIDs are 'distinct'. However, we have
|
|
|
|
// to create a new one if we add properties.
|
|
|
|
if (!MData || Args.size() > MData->getNumOperands()) {
|
|
|
|
MData = MDNode::getDistinct(Ctx, Args);
|
|
|
|
MData->replaceOperandWith(0, MData);
|
|
|
|
}
|
2021-03-04 17:46:29 +08:00
|
|
|
B->setMetadata(LLVMContext::MD_loop, MData);
|
2014-10-02 04:10:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 22:25:24 +08:00
|
|
|
/// Get the pointer operand
|
|
|
|
///
|
|
|
|
/// @param Inst The instruction to be analyzed.
|
|
|
|
/// @return the pointer operand in case @p Inst is a memory access
|
|
|
|
/// instruction and nullptr otherwise.
|
|
|
|
static llvm::Value *getMemAccInstPointerOperand(Instruction *Inst) {
|
|
|
|
auto MemInst = MemAccInst::dyn_cast(Inst);
|
|
|
|
if (!MemInst)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return MemInst.getPointerOperand();
|
|
|
|
}
|
|
|
|
|
2020-12-08 04:39:32 +08:00
|
|
|
/// Find the base pointer of an array access.
|
|
|
|
///
|
|
|
|
/// This should be equivalent to ScalarEvolution::getPointerBase, which we
|
|
|
|
/// cannot use here the IR is still under construction which ScalarEvolution
|
|
|
|
/// assumes to not be modified.
|
|
|
|
static Value *findBasePtr(Value *Val) {
|
|
|
|
while (true) {
|
|
|
|
if (auto *Gep = dyn_cast<GEPOperator>(Val)) {
|
|
|
|
Val = Gep->getPointerOperand();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto *Cast = dyn_cast<BitCastOperator>(Val)) {
|
|
|
|
Val = Cast->getOperand(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2014-10-02 23:32:17 +08:00
|
|
|
void ScopAnnotator::annotate(Instruction *Inst) {
|
2014-10-02 23:31:24 +08:00
|
|
|
if (!Inst->mayReadOrWriteMemory())
|
|
|
|
return;
|
|
|
|
|
2021-03-04 17:46:29 +08:00
|
|
|
switch (ParallelLoops.size()) {
|
|
|
|
case 0:
|
|
|
|
// Not parallel to anything: no access group needed.
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Single parallel loop: use directly.
|
|
|
|
Inst->setMetadata(LLVMContext::MD_access_group,
|
|
|
|
cast<MDNode>(ParallelLoops.front()));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Parallel to multiple loops: refer to list of access groups.
|
|
|
|
Inst->setMetadata(LLVMContext::MD_access_group,
|
|
|
|
MDNode::get(SE->getContext(),
|
|
|
|
ArrayRef<Metadata *>(
|
|
|
|
(Metadata *const *)ParallelLoops.data(),
|
|
|
|
ParallelLoops.size())));
|
|
|
|
break;
|
|
|
|
}
|
2015-10-07 21:19:03 +08:00
|
|
|
|
2014-10-02 23:31:24 +08:00
|
|
|
// TODO: Use the ScopArrayInfo once available here.
|
2015-10-07 21:19:03 +08:00
|
|
|
if (!AliasScopeDomain)
|
|
|
|
return;
|
2014-10-02 23:31:24 +08:00
|
|
|
|
2017-12-23 01:44:53 +08:00
|
|
|
// Do not apply annotations on memory operations that take more than one
|
|
|
|
// pointer. It would be ambiguous to which pointer the annotation applies.
|
|
|
|
// FIXME: How can we specify annotations for all pointer arguments?
|
|
|
|
if (isa<CallInst>(Inst) && !isa<MemSetInst>(Inst))
|
|
|
|
return;
|
|
|
|
|
2017-03-22 22:25:24 +08:00
|
|
|
auto *Ptr = getMemAccInstPointerOperand(Inst);
|
2016-02-25 22:08:48 +08:00
|
|
|
if (!Ptr)
|
|
|
|
return;
|
|
|
|
|
2020-12-08 04:39:32 +08:00
|
|
|
Value *BasePtr = findBasePtr(Ptr);
|
2015-10-07 21:19:03 +08:00
|
|
|
if (!BasePtr)
|
2014-03-04 22:59:00 +08:00
|
|
|
return;
|
|
|
|
|
2015-10-07 21:35:20 +08:00
|
|
|
auto AliasScope = AliasScopeMap.lookup(BasePtr);
|
2015-10-07 21:19:03 +08:00
|
|
|
|
2015-10-07 21:35:20 +08:00
|
|
|
if (!AliasScope) {
|
|
|
|
BasePtr = AlternativeAliasBases.lookup(BasePtr);
|
|
|
|
if (!BasePtr)
|
2015-10-07 21:19:06 +08:00
|
|
|
return;
|
2015-10-07 21:19:03 +08:00
|
|
|
|
2015-10-07 21:35:20 +08:00
|
|
|
AliasScope = AliasScopeMap.lookup(BasePtr);
|
|
|
|
if (!AliasScope)
|
2015-10-07 21:19:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(OtherAliasScopeListMap.count(BasePtr) &&
|
|
|
|
"BasePtr either expected in AliasScopeMap and OtherAlias...Map");
|
2015-10-07 21:19:03 +08:00
|
|
|
auto *OtherAliasScopeList = OtherAliasScopeListMap[BasePtr];
|
|
|
|
|
2021-09-21 04:19:51 +08:00
|
|
|
Inst->setMetadata("alias.scope", MDNode::get(SE->getContext(), AliasScope));
|
2015-10-07 21:19:03 +08:00
|
|
|
Inst->setMetadata("noalias", OtherAliasScopeList);
|
2014-03-04 22:59:00 +08:00
|
|
|
}
|