2017-08-25 05:22:41 +08:00
|
|
|
//===- IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST -------===//
|
2015-04-27 20:32:24 +08:00
|
|
|
//
|
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
|
2015-04-27 20:32:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the IslNodeBuilder, a class to translate an isl AST into
|
|
|
|
// a LLVM-IR AST.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/CodeGen/IslNodeBuilder.h"
|
|
|
|
#include "polly/CodeGen/BlockGenerators.h"
|
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
|
|
|
#include "polly/CodeGen/IslAst.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/CodeGen/IslExprBuilder.h"
|
2019-03-19 11:18:21 +08:00
|
|
|
#include "polly/CodeGen/LoopGeneratorsGOMP.h"
|
|
|
|
#include "polly/CodeGen/LoopGeneratorsKMP.h"
|
2016-11-18 05:49:19 +08:00
|
|
|
#include "polly/CodeGen/RuntimeDebugBuilder.h"
|
|
|
|
#include "polly/Options.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2018-07-17 15:08:01 +08:00
|
|
|
#include "polly/Support/ISLTools.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/aff.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/aff_type.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "isl/ast.h"
|
|
|
|
#include "isl/ast_build.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/isl-noexceptions.h"
|
2015-04-27 20:32:24 +08:00
|
|
|
#include "isl/map.h"
|
2015-05-09 17:13:42 +08:00
|
|
|
#include "isl/set.h"
|
|
|
|
#include "isl/union_map.h"
|
2015-05-09 17:36:38 +08:00
|
|
|
#include "isl/union_set.h"
|
2017-08-25 05:22:41 +08:00
|
|
|
#include "isl/val.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2017-08-25 05:22:41 +08:00
|
|
|
using namespace polly;
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2016-11-18 05:55:43 +08:00
|
|
|
#define DEBUG_TYPE "polly-codegen"
|
|
|
|
|
2016-11-18 22:37:08 +08:00
|
|
|
STATISTIC(VersionedScops, "Number of SCoPs that required versioning.");
|
2016-11-18 05:55:43 +08:00
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
STATISTIC(SequentialLoops, "Number of generated sequential for-loops");
|
|
|
|
STATISTIC(ParallelLoops, "Number of generated parallel for-loops");
|
|
|
|
STATISTIC(VectorLoops, "Number of generated vector for-loops");
|
|
|
|
STATISTIC(IfConditions, "Number of generated if-conditions");
|
|
|
|
|
2019-03-19 11:18:21 +08:00
|
|
|
/// OpenMP backend options
|
|
|
|
enum class OpenMPBackend { GNU, LLVM };
|
|
|
|
|
2016-11-18 05:49:19 +08:00
|
|
|
static cl::opt<bool> PollyGenerateRTCPrint(
|
|
|
|
"polly-codegen-emit-rtc-print",
|
|
|
|
cl::desc("Emit code that prints the runtime check result dynamically."),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2016-11-23 04:21:16 +08:00
|
|
|
// If this option is set we always use the isl AST generator to regenerate
|
|
|
|
// memory accesses. Without this option set we regenerate expressions using the
|
|
|
|
// original SCEV expressions and only generate new expressions in case the
|
|
|
|
// access relation has been changed and consequently must be regenerated.
|
|
|
|
static cl::opt<bool> PollyGenerateExpressions(
|
|
|
|
"polly-codegen-generate-expressions",
|
|
|
|
cl::desc("Generate AST expressions for unmodified and modified accesses"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2016-12-21 20:37:36 +08:00
|
|
|
static cl::opt<int> PollyTargetFirstLevelCacheLineSize(
|
|
|
|
"polly-target-first-level-cache-line-size",
|
|
|
|
cl::desc("The size of the first level cache line size specified in bytes."),
|
|
|
|
cl::Hidden, cl::init(64), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2019-03-19 11:18:21 +08:00
|
|
|
static cl::opt<OpenMPBackend> PollyOmpBackend(
|
|
|
|
"polly-omp-backend", cl::desc("Choose the OpenMP library to use:"),
|
|
|
|
cl::values(clEnumValN(OpenMPBackend::GNU, "GNU", "GNU OpenMP"),
|
|
|
|
clEnumValN(OpenMPBackend::LLVM, "LLVM", "LLVM OpenMP")),
|
|
|
|
cl::Hidden, cl::init(OpenMPBackend::GNU), cl::cat(PollyCategory));
|
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
isl::ast_expr IslNodeBuilder::getUpperBound(isl::ast_node For,
|
|
|
|
ICmpInst::Predicate &Predicate) {
|
|
|
|
isl::ast_expr Cond = For.for_get_cond();
|
|
|
|
isl::ast_expr Iterator = For.for_get_iterator();
|
|
|
|
assert(isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op &&
|
2015-04-27 20:32:24 +08:00
|
|
|
"conditional expression is not an atomic upper bound");
|
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
isl_ast_op_type OpType = isl_ast_expr_get_op_type(Cond.get());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
switch (OpType) {
|
2015-04-27 20:32:24 +08:00
|
|
|
case isl_ast_op_le:
|
|
|
|
Predicate = ICmpInst::ICMP_SLE;
|
|
|
|
break;
|
|
|
|
case isl_ast_op_lt:
|
|
|
|
Predicate = ICmpInst::ICMP_SLT;
|
|
|
|
break;
|
|
|
|
default:
|
2017-06-08 20:06:15 +08:00
|
|
|
llvm_unreachable("Unexpected comparison type in loop condition");
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
isl::ast_expr Arg0 = Cond.get_op_arg(0);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
assert(isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id &&
|
2015-04-27 20:32:24 +08:00
|
|
|
"conditional expression is not an atomic upper bound");
|
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
isl::id UBID = Arg0.get_id();
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
assert(isl_ast_expr_get_type(Iterator.get()) == isl_ast_expr_id &&
|
2015-04-27 20:32:24 +08:00
|
|
|
"Could not get the iterator");
|
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
isl::id IteratorID = Iterator.get_id();
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
assert(UBID.get() == IteratorID.get() &&
|
2015-04-27 20:32:24 +08:00
|
|
|
"conditional expression is not an atomic upper bound");
|
|
|
|
|
2018-08-01 06:42:59 +08:00
|
|
|
return Cond.get_op_arg(1);
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Return true if a return value of Predicate is true for the value represented
|
|
|
|
/// by passed isl_ast_expr_int.
|
2015-08-21 17:08:14 +08:00
|
|
|
static bool checkIslAstExprInt(__isl_take isl_ast_expr *Expr,
|
|
|
|
isl_bool (*Predicate)(__isl_keep isl_val *)) {
|
|
|
|
if (isl_ast_expr_get_type(Expr) != isl_ast_expr_int) {
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return false;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
2015-08-21 17:08:14 +08:00
|
|
|
auto ExprVal = isl_ast_expr_get_val(Expr);
|
|
|
|
isl_ast_expr_free(Expr);
|
2017-08-25 05:22:41 +08:00
|
|
|
if (Predicate(ExprVal) != isl_bool_true) {
|
2015-08-21 17:08:14 +08:00
|
|
|
isl_val_free(ExprVal);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
isl_val_free(ExprVal);
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 07:01:50 +08:00
|
|
|
int IslNodeBuilder::getNumberOfIterations(isl::ast_node For) {
|
|
|
|
assert(isl_ast_node_get_type(For.get()) == isl_ast_node_for);
|
|
|
|
isl::ast_node Body = For.for_get_body();
|
2015-08-25 04:11:34 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
// First, check if we can actually handle this code.
|
2018-08-01 07:01:50 +08:00
|
|
|
switch (isl_ast_node_get_type(Body.get())) {
|
2015-08-25 04:11:34 +08:00
|
|
|
case isl_ast_node_user:
|
|
|
|
break;
|
|
|
|
case isl_ast_node_block: {
|
2018-08-01 07:01:50 +08:00
|
|
|
isl::ast_node_list List = Body.block_get_children();
|
|
|
|
for (isl::ast_node Node : List) {
|
|
|
|
isl_ast_node_type NodeType = isl_ast_node_get_type(Node.get());
|
|
|
|
if (NodeType != isl_ast_node_user)
|
2015-08-25 04:11:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:01:50 +08:00
|
|
|
isl::ast_expr Init = For.for_get_init();
|
|
|
|
if (!checkIslAstExprInt(Init.release(), isl_val_is_zero))
|
2015-08-21 17:08:14 +08:00
|
|
|
return -1;
|
2018-08-01 07:01:50 +08:00
|
|
|
isl::ast_expr Inc = For.for_get_inc();
|
|
|
|
if (!checkIslAstExprInt(Inc.release(), isl_val_is_one))
|
2015-08-21 17:08:14 +08:00
|
|
|
return -1;
|
|
|
|
CmpInst::Predicate Predicate;
|
2018-08-01 07:01:50 +08:00
|
|
|
isl::ast_expr UB = getUpperBound(For, Predicate);
|
|
|
|
if (isl_ast_expr_get_type(UB.get()) != isl_ast_expr_int)
|
2015-08-21 17:08:14 +08:00
|
|
|
return -1;
|
2018-08-01 07:01:50 +08:00
|
|
|
isl::val UpVal = UB.get_val();
|
|
|
|
int NumberIterations = UpVal.get_num_si();
|
2015-08-21 17:08:14 +08:00
|
|
|
if (NumberIterations < 0)
|
2015-04-27 20:32:24 +08:00
|
|
|
return -1;
|
2015-08-21 17:08:14 +08:00
|
|
|
if (Predicate == CmpInst::ICMP_SLT)
|
|
|
|
return NumberIterations;
|
|
|
|
else
|
|
|
|
return NumberIterations + 1;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Extract the values and SCEVs needed to generate code for a block.
|
2015-09-05 23:45:25 +08:00
|
|
|
static int findReferencesInBlock(struct SubtreeReferences &References,
|
2017-08-17 05:37:53 +08:00
|
|
|
const ScopStmt *Stmt, BasicBlock *BB) {
|
|
|
|
for (Instruction &Inst : *BB) {
|
|
|
|
// Include invariant loads
|
|
|
|
if (isa<LoadInst>(Inst))
|
|
|
|
if (Value *InvariantLoad = References.GlobalMap.lookup(&Inst))
|
|
|
|
References.Values.insert(InvariantLoad);
|
|
|
|
|
2016-03-02 05:44:06 +08:00
|
|
|
for (Value *SrcVal : Inst.operands()) {
|
|
|
|
auto *Scope = References.LI.getLoopFor(BB);
|
2016-11-29 23:11:04 +08:00
|
|
|
if (canSynthesize(SrcVal, References.S, &References.SE, Scope)) {
|
2016-03-02 05:44:06 +08:00
|
|
|
References.SCEVs.insert(References.SE.getSCEVAtScope(SrcVal, Scope));
|
2015-08-31 17:05:43 +08:00
|
|
|
continue;
|
2015-10-02 21:11:27 +08:00
|
|
|
} else if (Value *NewVal = References.GlobalMap.lookup(SrcVal))
|
|
|
|
References.Values.insert(NewVal);
|
2016-03-02 05:44:06 +08:00
|
|
|
}
|
2017-08-17 05:37:53 +08:00
|
|
|
}
|
2015-04-27 20:32:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:08:01 +08:00
|
|
|
void addReferencesFromStmt(const ScopStmt *Stmt, void *UserPtr,
|
|
|
|
bool CreateScalarRefs) {
|
2015-09-05 23:45:25 +08:00
|
|
|
auto &References = *static_cast<struct SubtreeReferences *>(UserPtr);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
if (Stmt->isBlockStmt())
|
2015-09-05 23:45:25 +08:00
|
|
|
findReferencesInBlock(References, Stmt, Stmt->getBasicBlock());
|
2015-04-27 20:32:24 +08:00
|
|
|
else {
|
|
|
|
assert(Stmt->isRegionStmt() &&
|
|
|
|
"Stmt was neither block nor region statement");
|
2017-08-17 05:37:53 +08:00
|
|
|
for (BasicBlock *BB : Stmt->getRegion()->blocks())
|
2015-09-05 23:45:25 +08:00
|
|
|
findReferencesInBlock(References, Stmt, BB);
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 13:52:24 +08:00
|
|
|
for (auto &Access : *Stmt) {
|
2017-08-19 20:58:28 +08:00
|
|
|
if (References.ParamSpace) {
|
|
|
|
isl::space ParamSpace = Access->getLatestAccessRelation().get_space();
|
|
|
|
(*References.ParamSpace) =
|
|
|
|
References.ParamSpace->align_params(ParamSpace);
|
|
|
|
}
|
|
|
|
|
2017-08-09 20:27:51 +08:00
|
|
|
if (Access->isLatestArrayKind()) {
|
2017-10-31 18:28:22 +08:00
|
|
|
auto *BasePtr = Access->getLatestScopArrayInfo()->getBasePtr();
|
2015-08-31 13:52:24 +08:00
|
|
|
if (Instruction *OpInst = dyn_cast<Instruction>(BasePtr))
|
2016-05-23 20:40:48 +08:00
|
|
|
if (Stmt->getParent()->contains(OpInst))
|
2015-08-31 13:52:24 +08:00
|
|
|
continue;
|
|
|
|
|
2015-09-05 23:45:25 +08:00
|
|
|
References.Values.insert(BasePtr);
|
2015-08-31 13:52:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-04 14:55:59 +08:00
|
|
|
if (CreateScalarRefs)
|
|
|
|
References.Values.insert(References.BlockGen.getOrCreateAlloca(*Access));
|
2015-08-31 13:52:24 +08:00
|
|
|
}
|
2015-09-05 23:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the out-of-scop values and SCEVs referenced from a set describing
|
|
|
|
/// a ScopStmt.
|
|
|
|
///
|
|
|
|
/// This includes the SCEVUnknowns referenced by the SCEVs used in the
|
|
|
|
/// statement and the base pointers of the memory accesses. For scalar
|
|
|
|
/// statements we force the generation of alloca memory locations and list
|
|
|
|
/// these locations in the set of out-of-scop values as well.
|
|
|
|
///
|
|
|
|
/// @param Set A set which references the ScopStmt we are interested in.
|
|
|
|
/// @param UserPtr A void pointer that can be casted to a SubtreeReferences
|
|
|
|
/// structure.
|
2018-07-17 15:08:01 +08:00
|
|
|
static void addReferencesFromStmtSet(isl::set Set,
|
|
|
|
struct SubtreeReferences *UserPtr) {
|
|
|
|
isl::id Id = Set.get_tuple_id();
|
|
|
|
auto *Stmt = static_cast<const ScopStmt *>(Id.get_user());
|
2015-09-05 23:45:25 +08:00
|
|
|
return addReferencesFromStmt(Stmt, UserPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the out-of-scop values and SCEVs referenced from a union set
|
|
|
|
/// referencing multiple ScopStmts.
|
|
|
|
///
|
|
|
|
/// This includes the SCEVUnknowns referenced by the SCEVs used in the
|
|
|
|
/// statement and the base pointers of the memory accesses. For scalar
|
|
|
|
/// statements we force the generation of alloca memory locations and list
|
|
|
|
/// these locations in the set of out-of-scop values as well.
|
|
|
|
///
|
|
|
|
/// @param USet A union set referencing the ScopStmts we are interested
|
|
|
|
/// in.
|
|
|
|
/// @param References The SubtreeReferences data structure through which
|
|
|
|
/// results are returned and further information is
|
|
|
|
/// provided.
|
|
|
|
static void
|
2018-07-17 15:08:01 +08:00
|
|
|
addReferencesFromStmtUnionSet(isl::union_set USet,
|
2015-09-05 23:45:25 +08:00
|
|
|
struct SubtreeReferences &References) {
|
2018-07-17 15:08:01 +08:00
|
|
|
|
|
|
|
for (isl::set Set : USet.get_set_list())
|
|
|
|
addReferencesFromStmtSet(Set, &References);
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:38 +08:00
|
|
|
__isl_give isl_union_map *
|
|
|
|
IslNodeBuilder::getScheduleForAstNode(__isl_keep isl_ast_node *For) {
|
|
|
|
return IslAstInfo::getSchedule(For);
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For,
|
|
|
|
SetVector<Value *> &Values,
|
|
|
|
SetVector<const Loop *> &Loops) {
|
|
|
|
SetVector<const SCEV *> SCEVs;
|
2015-10-02 21:11:27 +08:00
|
|
|
struct SubtreeReferences References = {
|
2017-08-19 20:58:28 +08:00
|
|
|
LI, SE, S, ValueMap, Values, SCEVs, getBlockGenerator(), nullptr};
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
for (const auto &I : IDToValue)
|
|
|
|
Values.insert(I.second);
|
|
|
|
|
2017-08-06 10:39:05 +08:00
|
|
|
// NOTE: this is populated in IslNodeBuilder::addParameters
|
2015-04-27 20:32:24 +08:00
|
|
|
for (const auto &I : OutsideLoopIterations)
|
|
|
|
Values.insert(cast<SCEVUnknown>(I.second)->getValue());
|
|
|
|
|
2018-07-17 15:08:01 +08:00
|
|
|
isl::union_set Schedule =
|
|
|
|
isl::manage(isl_union_map_domain(getScheduleForAstNode(For)));
|
2015-09-05 23:45:25 +08:00
|
|
|
addReferencesFromStmtUnionSet(Schedule, References);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
for (const SCEV *Expr : SCEVs) {
|
2016-04-08 18:25:58 +08:00
|
|
|
findValues(Expr, SE, Values);
|
2015-04-27 20:32:24 +08:00
|
|
|
findLoops(Expr, Loops);
|
|
|
|
}
|
|
|
|
|
|
|
|
Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); });
|
|
|
|
|
2017-08-06 10:07:11 +08:00
|
|
|
/// Note: Code generation of induction variables of loops outside Scops
|
|
|
|
///
|
2015-04-27 20:32:24 +08:00
|
|
|
/// Remove loops that contain the scop or that are part of the scop, as they
|
|
|
|
/// are considered local. This leaves only loops that are before the scop, but
|
|
|
|
/// do not contain the scop itself.
|
2017-08-06 10:07:11 +08:00
|
|
|
/// We ignore loops perfectly contained in the Scop because these are already
|
|
|
|
/// generated at `IslNodeBuilder::addParameters`. These `Loops` are loops
|
|
|
|
/// whose induction variables are referred to by the Scop, but the Scop is not
|
|
|
|
/// fully contained in these Loops. Since there can be many of these,
|
|
|
|
/// we choose to codegen these on-demand.
|
|
|
|
/// @see IslNodeBuilder::materializeNonScopLoopInductionVariable.
|
2015-04-27 20:32:24 +08:00
|
|
|
Loops.remove_if([this](const Loop *L) {
|
2016-05-23 20:42:38 +08:00
|
|
|
return S.contains(L) || L->contains(S.getEntry());
|
2015-04-27 20:32:24 +08:00
|
|
|
});
|
2017-07-13 20:18:56 +08:00
|
|
|
|
|
|
|
// Contains Values that may need to be replaced with other values
|
|
|
|
// due to replacements from the ValueMap. We should make sure
|
|
|
|
// that we return correctly remapped values.
|
|
|
|
// NOTE: this code path is tested by:
|
|
|
|
// 1. test/Isl/CodeGen/OpenMP/single_loop_with_loop_invariant_baseptr.ll
|
|
|
|
// 2. test/Isl/CodeGen/OpenMP/loop-body-references-outer-values-3.ll
|
|
|
|
SetVector<Value *> ReplacedValues;
|
|
|
|
for (Value *V : Values) {
|
2017-08-01 20:15:51 +08:00
|
|
|
ReplacedValues.insert(getLatestValue(V));
|
2017-07-13 20:18:56 +08:00
|
|
|
}
|
|
|
|
Values = ReplacedValues;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 04:15:56 +08:00
|
|
|
void IslNodeBuilder::updateValues(ValueMapT &NewValues) {
|
2015-04-27 20:32:24 +08:00
|
|
|
SmallPtrSet<Value *, 5> Inserted;
|
|
|
|
|
|
|
|
for (const auto &I : IDToValue) {
|
|
|
|
IDToValue[I.first] = NewValues[I.second];
|
|
|
|
Inserted.insert(I.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &I : NewValues) {
|
|
|
|
if (Inserted.count(I.first))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ValueMap[I.first] = I.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:15:51 +08:00
|
|
|
Value *IslNodeBuilder::getLatestValue(Value *Original) const {
|
|
|
|
auto It = ValueMap.find(Original);
|
|
|
|
if (It == ValueMap.end())
|
|
|
|
return Original;
|
|
|
|
return It->second;
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
|
|
|
|
std::vector<Value *> &IVS,
|
|
|
|
__isl_take isl_id *IteratorID,
|
|
|
|
__isl_take isl_union_map *Schedule) {
|
|
|
|
isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
|
|
|
|
isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
isl_id *Id = isl_ast_expr_get_id(StmtExpr);
|
|
|
|
isl_ast_expr_free(StmtExpr);
|
|
|
|
ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
|
|
|
|
std::vector<LoopToScevMapT> VLTS(IVS.size());
|
|
|
|
|
2017-08-07 00:39:52 +08:00
|
|
|
isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain().release());
|
2015-04-27 20:32:24 +08:00
|
|
|
Schedule = isl_union_map_intersect_domain(Schedule, Domain);
|
|
|
|
isl_map *S = isl_map_from_union_map(Schedule);
|
|
|
|
|
2015-09-05 21:03:57 +08:00
|
|
|
auto *NewAccesses = createNewAccesses(Stmt, User);
|
2015-09-05 17:56:54 +08:00
|
|
|
createSubstitutionsVector(Expr, Stmt, VLTS, IVS, IteratorID);
|
|
|
|
VectorBlockGenerator::generate(BlockGen, *Stmt, VLTS, S, NewAccesses);
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr_free(NewAccesses);
|
2015-04-27 20:32:24 +08:00
|
|
|
isl_map_free(S);
|
|
|
|
isl_id_free(Id);
|
|
|
|
isl_ast_node_free(User);
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:11:00 +08:00
|
|
|
void IslNodeBuilder::createMark(__isl_take isl_ast_node *Node) {
|
2016-02-23 17:00:13 +08:00
|
|
|
auto *Id = isl_ast_node_mark_get_id(Node);
|
2015-08-23 17:11:00 +08:00
|
|
|
auto Child = isl_ast_node_mark_get_node(Node);
|
|
|
|
isl_ast_node_free(Node);
|
2016-02-23 17:00:13 +08:00
|
|
|
// If a child node of a 'SIMD mark' is a loop that has a single iteration,
|
|
|
|
// it will be optimized away and we should skip it.
|
2017-08-25 05:22:41 +08:00
|
|
|
if (strcmp(isl_id_get_name(Id), "SIMD") == 0 &&
|
2016-02-23 17:00:13 +08:00
|
|
|
isl_ast_node_get_type(Child) == isl_ast_node_for) {
|
|
|
|
bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
|
2018-08-01 07:01:50 +08:00
|
|
|
int VectorWidth = getNumberOfIterations(isl::manage_copy(Child));
|
2016-02-23 17:00:13 +08:00
|
|
|
if (Vector && 1 < VectorWidth && VectorWidth <= 16)
|
|
|
|
createForVector(Child, VectorWidth);
|
|
|
|
else
|
2018-08-01 06:43:04 +08:00
|
|
|
createForSequential(isl::manage(Child), true);
|
2016-02-23 17:00:13 +08:00
|
|
|
isl_id_free(Id);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-25 05:22:41 +08:00
|
|
|
if (strcmp(isl_id_get_name(Id), "Inter iteration alias-free") == 0) {
|
2017-03-22 22:25:24 +08:00
|
|
|
auto *BasePtr = static_cast<Value *>(isl_id_get_user(Id));
|
|
|
|
Annotator.addInterIterationAliasFreeBasePtr(BasePtr);
|
|
|
|
}
|
2016-02-23 17:00:13 +08:00
|
|
|
create(Child);
|
|
|
|
isl_id_free(Id);
|
2015-08-23 17:11:00 +08:00
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
|
|
|
|
int VectorWidth) {
|
|
|
|
isl_ast_node *Body = isl_ast_node_for_get_body(For);
|
|
|
|
isl_ast_expr *Init = isl_ast_node_for_get_init(For);
|
|
|
|
isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
|
|
|
|
isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
|
|
|
|
isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
|
|
|
|
|
|
|
|
Value *ValueLB = ExprBuilder.create(Init);
|
|
|
|
Value *ValueInc = ExprBuilder.create(Inc);
|
|
|
|
|
2016-06-12 03:17:15 +08:00
|
|
|
Type *MaxType = ExprBuilder.getType(Iterator);
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
|
2016-06-06 17:57:41 +08:00
|
|
|
|
2016-06-12 03:17:15 +08:00
|
|
|
if (MaxType != ValueLB->getType())
|
|
|
|
ValueLB = Builder.CreateSExt(ValueLB, MaxType);
|
|
|
|
if (MaxType != ValueInc->getType())
|
|
|
|
ValueInc = Builder.CreateSExt(ValueInc, MaxType);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
std::vector<Value *> IVS(VectorWidth);
|
|
|
|
IVS[0] = ValueLB;
|
|
|
|
|
|
|
|
for (int i = 1; i < VectorWidth; i++)
|
|
|
|
IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
|
|
|
|
|
2015-09-09 17:24:38 +08:00
|
|
|
isl_union_map *Schedule = getScheduleForAstNode(For);
|
2015-04-27 20:32:24 +08:00
|
|
|
assert(Schedule && "For statement annotation does not contain its schedule");
|
|
|
|
|
|
|
|
IDToValue[IteratorID] = ValueLB;
|
|
|
|
|
|
|
|
switch (isl_ast_node_get_type(Body)) {
|
|
|
|
case isl_ast_node_user:
|
|
|
|
createUserVector(Body, IVS, isl_id_copy(IteratorID),
|
|
|
|
isl_union_map_copy(Schedule));
|
|
|
|
break;
|
|
|
|
case isl_ast_node_block: {
|
|
|
|
isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
|
|
|
|
|
|
|
|
for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
|
|
|
|
createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
|
|
|
|
isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
|
|
|
|
|
|
|
|
isl_ast_node_free(Body);
|
|
|
|
isl_ast_node_list_free(List);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
isl_ast_node_dump(Body);
|
|
|
|
llvm_unreachable("Unhandled isl_ast_node in vectorizer");
|
|
|
|
}
|
|
|
|
|
|
|
|
IDToValue.erase(IDToValue.find(IteratorID));
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
isl_union_map_free(Schedule);
|
|
|
|
|
|
|
|
isl_ast_node_free(For);
|
|
|
|
isl_ast_expr_free(Iterator);
|
2017-08-23 21:50:30 +08:00
|
|
|
|
|
|
|
VectorLoops++;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:38:46 +08:00
|
|
|
/// Restore the initial ordering of dimensions of the band node
|
|
|
|
///
|
|
|
|
/// In case the band node represents all the dimensions of the iteration
|
|
|
|
/// domain, recreate the band node to restore the initial ordering of the
|
|
|
|
/// dimensions.
|
|
|
|
///
|
|
|
|
/// @param Node The band node to be modified.
|
|
|
|
/// @return The modified schedule node.
|
2017-08-25 05:22:41 +08:00
|
|
|
static bool IsLoopVectorizerDisabled(isl::ast_node Node) {
|
2018-04-29 08:28:26 +08:00
|
|
|
assert(isl_ast_node_get_type(Node.get()) == isl_ast_node_for);
|
2017-08-23 01:38:46 +08:00
|
|
|
auto Body = Node.for_get_body();
|
2018-04-29 08:28:26 +08:00
|
|
|
if (isl_ast_node_get_type(Body.get()) != isl_ast_node_mark)
|
2017-08-23 01:38:46 +08:00
|
|
|
return false;
|
|
|
|
auto Id = Body.mark_get_id();
|
2017-08-25 05:22:41 +08:00
|
|
|
if (strcmp(Id.get_name().c_str(), "Loop Vectorizer Disabled") == 0)
|
2017-08-23 01:38:46 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
void IslNodeBuilder::createForSequential(isl::ast_node For, bool MarkParallel) {
|
2015-04-27 20:32:24 +08:00
|
|
|
Value *ValueLB, *ValueUB, *ValueInc;
|
2016-06-12 03:17:15 +08:00
|
|
|
Type *MaxType;
|
2015-04-27 20:32:24 +08:00
|
|
|
BasicBlock *ExitBlock;
|
|
|
|
Value *IV;
|
|
|
|
CmpInst::Predicate Predicate;
|
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
bool LoopVectorizerDisabled = IsLoopVectorizerDisabled(For);
|
2017-08-23 01:38:46 +08:00
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
isl::ast_node Body = For.for_get_body();
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
// isl_ast_node_for_is_degenerate(For)
|
|
|
|
//
|
|
|
|
// TODO: For degenerated loops we could generate a plain assignment.
|
|
|
|
// However, for now we just reuse the logic for normal loops, which will
|
|
|
|
// create a loop with a single iteration.
|
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
isl::ast_expr Init = For.for_get_init();
|
|
|
|
isl::ast_expr Inc = For.for_get_inc();
|
|
|
|
isl::ast_expr Iterator = For.for_get_iterator();
|
|
|
|
isl::id IteratorID = Iterator.get_id();
|
|
|
|
isl::ast_expr UB = getUpperBound(For, Predicate);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
ValueLB = ExprBuilder.create(Init.release());
|
|
|
|
ValueUB = ExprBuilder.create(UB.release());
|
|
|
|
ValueInc = ExprBuilder.create(Inc.release());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
MaxType = ExprBuilder.getType(Iterator.get());
|
2016-06-12 03:17:15 +08:00
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
|
|
|
|
|
|
|
|
if (MaxType != ValueLB->getType())
|
|
|
|
ValueLB = Builder.CreateSExt(ValueLB, MaxType);
|
|
|
|
if (MaxType != ValueUB->getType())
|
|
|
|
ValueUB = Builder.CreateSExt(ValueUB, MaxType);
|
|
|
|
if (MaxType != ValueInc->getType())
|
|
|
|
ValueInc = Builder.CreateSExt(ValueInc, MaxType);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
// If we can show that LB <Predicate> UB holds at least once, we can
|
|
|
|
// omit the GuardBB in front of the loop.
|
|
|
|
bool UseGuardBB =
|
|
|
|
!SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB));
|
2017-04-04 18:01:53 +08:00
|
|
|
IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, LI, DT, ExitBlock,
|
2017-10-30 05:36:34 +08:00
|
|
|
Predicate, &Annotator, MarkParallel, UseGuardBB,
|
2017-08-23 01:38:46 +08:00
|
|
|
LoopVectorizerDisabled);
|
2018-08-01 06:43:04 +08:00
|
|
|
IDToValue[IteratorID.get()] = IV;
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
create(Body.release());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2017-10-30 05:36:34 +08:00
|
|
|
Annotator.popLoop(MarkParallel);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2018-08-01 06:43:04 +08:00
|
|
|
IDToValue.erase(IDToValue.find(IteratorID.get()));
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&ExitBlock->front());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2017-08-23 21:50:30 +08:00
|
|
|
SequentialLoops++;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Remove the BBs contained in a (sub)function from the dominator tree.
|
2015-04-27 20:32:24 +08:00
|
|
|
///
|
|
|
|
/// This function removes the basic blocks that are part of a subfunction from
|
|
|
|
/// the dominator tree. Specifically, when generating code it may happen that at
|
|
|
|
/// some point the code generation continues in a new sub-function (e.g., when
|
|
|
|
/// generating OpenMP code). The basic blocks that are created in this
|
|
|
|
/// sub-function are then still part of the dominator tree of the original
|
|
|
|
/// function, such that the dominator tree reaches over function boundaries.
|
|
|
|
/// This is not only incorrect, but also causes crashes. This function now
|
|
|
|
/// removes from the dominator tree all basic blocks that are dominated (and
|
|
|
|
/// consequently reachable) from the entry block of this (sub)function.
|
|
|
|
///
|
|
|
|
/// FIXME: A LLVM (function or region) pass should not touch anything outside of
|
|
|
|
/// the function/region it runs on. Hence, the pure need for this function shows
|
|
|
|
/// that we do not comply to this rule. At the moment, this does not cause any
|
|
|
|
/// issues, but we should be aware that such issues may appear. Unfortunately
|
|
|
|
/// the current LLVM pass infrastructure does not allow to make Polly a module
|
|
|
|
/// or call-graph pass to solve this issue, as such a pass would not have access
|
|
|
|
/// to the per-function analyses passes needed by Polly. A future pass manager
|
|
|
|
/// infrastructure is supposed to enable such kind of access possibly allowing
|
|
|
|
/// us to create a cleaner solution here.
|
|
|
|
///
|
|
|
|
/// FIXME: Instead of adding the dominance information and then dropping it
|
|
|
|
/// later on, we should try to just not add it in the first place. This requires
|
|
|
|
/// some careful testing to make sure this does not break in interaction with
|
|
|
|
/// the SCEVBuilder and SplitBlock which may rely on the dominator tree or
|
|
|
|
/// which may try to update it.
|
|
|
|
///
|
|
|
|
/// @param F The function which contains the BBs to removed.
|
|
|
|
/// @param DT The dominator tree from which to remove the BBs.
|
|
|
|
static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) {
|
|
|
|
DomTreeNode *N = DT.getNode(&F->getEntryBlock());
|
|
|
|
std::vector<BasicBlock *> Nodes;
|
|
|
|
|
|
|
|
// We can only remove an element from the dominator tree, if all its children
|
|
|
|
// have been removed. To ensure this we obtain the list of nodes to remove
|
|
|
|
// using a post-order tree traversal.
|
|
|
|
for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
|
|
|
|
Nodes.push_back(I->getBlock());
|
|
|
|
|
|
|
|
for (BasicBlock *BB : Nodes)
|
|
|
|
DT.eraseNode(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) {
|
|
|
|
isl_ast_node *Body;
|
|
|
|
isl_ast_expr *Init, *Inc, *Iterator, *UB;
|
|
|
|
isl_id *IteratorID;
|
|
|
|
Value *ValueLB, *ValueUB, *ValueInc;
|
2016-06-12 03:17:15 +08:00
|
|
|
Type *MaxType;
|
2015-04-27 20:32:24 +08:00
|
|
|
Value *IV;
|
|
|
|
CmpInst::Predicate Predicate;
|
|
|
|
|
2015-09-27 04:57:59 +08:00
|
|
|
// The preamble of parallel code interacts different than normal code with
|
|
|
|
// e.g., scalar initialization. Therefore, we ensure the parallel code is
|
|
|
|
// separated from the last basic block.
|
2015-11-07 06:56:54 +08:00
|
|
|
BasicBlock *ParBB = SplitBlock(Builder.GetInsertBlock(),
|
|
|
|
&*Builder.GetInsertPoint(), &DT, &LI);
|
2015-09-27 04:57:59 +08:00
|
|
|
ParBB->setName("polly.parallel.for");
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&ParBB->front());
|
2015-09-27 04:57:59 +08:00
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
Body = isl_ast_node_for_get_body(For);
|
|
|
|
Init = isl_ast_node_for_get_init(For);
|
|
|
|
Inc = isl_ast_node_for_get_inc(For);
|
|
|
|
Iterator = isl_ast_node_for_get_iterator(For);
|
|
|
|
IteratorID = isl_ast_expr_get_id(Iterator);
|
2018-08-01 06:42:59 +08:00
|
|
|
UB = getUpperBound(isl::manage_copy(For), Predicate).release();
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
ValueLB = ExprBuilder.create(Init);
|
|
|
|
ValueUB = ExprBuilder.create(UB);
|
|
|
|
ValueInc = ExprBuilder.create(Inc);
|
|
|
|
|
|
|
|
// OpenMP always uses SLE. In case the isl generated AST uses a SLT
|
2017-06-08 20:06:15 +08:00
|
|
|
// expression, we need to adjust the loop bound by one.
|
2015-04-27 20:32:24 +08:00
|
|
|
if (Predicate == CmpInst::ICMP_SLT)
|
|
|
|
ValueUB = Builder.CreateAdd(
|
|
|
|
ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType()));
|
|
|
|
|
2016-06-12 03:17:15 +08:00
|
|
|
MaxType = ExprBuilder.getType(Iterator);
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
|
|
|
|
MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
|
|
|
|
|
|
|
|
if (MaxType != ValueLB->getType())
|
|
|
|
ValueLB = Builder.CreateSExt(ValueLB, MaxType);
|
|
|
|
if (MaxType != ValueUB->getType())
|
|
|
|
ValueUB = Builder.CreateSExt(ValueUB, MaxType);
|
|
|
|
if (MaxType != ValueInc->getType())
|
|
|
|
ValueInc = Builder.CreateSExt(ValueInc, MaxType);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
BasicBlock::iterator LoopBody;
|
|
|
|
|
|
|
|
SetVector<Value *> SubtreeValues;
|
|
|
|
SetVector<const Loop *> Loops;
|
|
|
|
|
|
|
|
getReferencesInSubtree(For, SubtreeValues, Loops);
|
|
|
|
|
|
|
|
// Create for all loops we depend on values that contain the current loop
|
|
|
|
// iteration. These values are necessary to generate code for SCEVs that
|
|
|
|
// depend on such loops. As a result we need to pass them to the subfunction.
|
2017-08-06 10:07:11 +08:00
|
|
|
// See [Code generation of induction variables of loops outside Scops]
|
2015-04-27 20:32:24 +08:00
|
|
|
for (const Loop *L : Loops) {
|
2017-08-06 10:07:11 +08:00
|
|
|
Value *LoopInductionVar = materializeNonScopLoopInductionVariable(L);
|
|
|
|
SubtreeValues.insert(LoopInductionVar);
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 04:15:56 +08:00
|
|
|
ValueMapT NewValues;
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2019-03-19 11:18:21 +08:00
|
|
|
std::unique_ptr<ParallelLoopGenerator> ParallelLoopGenPtr;
|
|
|
|
|
|
|
|
switch (PollyOmpBackend) {
|
|
|
|
case OpenMPBackend::GNU:
|
|
|
|
ParallelLoopGenPtr.reset(
|
|
|
|
new ParallelLoopGeneratorGOMP(Builder, LI, DT, DL));
|
|
|
|
break;
|
|
|
|
case OpenMPBackend::LLVM:
|
|
|
|
ParallelLoopGenPtr.reset(new ParallelLoopGeneratorKMP(Builder, LI, DT, DL));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
IV = ParallelLoopGenPtr->createParallelLoop(
|
|
|
|
ValueLB, ValueUB, ValueInc, SubtreeValues, NewValues, &LoopBody);
|
2015-04-27 20:32:24 +08:00
|
|
|
BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&*LoopBody);
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2016-04-09 02:16:02 +08:00
|
|
|
// Remember the parallel subfunction
|
|
|
|
ParallelSubfunctions.push_back(LoopBody->getFunction());
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
// Save the current values.
|
2015-10-04 18:18:32 +08:00
|
|
|
auto ValueMapCopy = ValueMap;
|
2015-04-27 20:32:24 +08:00
|
|
|
IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue;
|
|
|
|
|
|
|
|
updateValues(NewValues);
|
|
|
|
IDToValue[IteratorID] = IV;
|
|
|
|
|
2015-10-08 04:15:56 +08:00
|
|
|
ValueMapT NewValuesReverse;
|
2015-08-20 00:04:35 +08:00
|
|
|
|
|
|
|
for (auto P : NewValues)
|
|
|
|
NewValuesReverse[P.second] = P.first;
|
|
|
|
|
|
|
|
Annotator.addAlternativeAliasBases(NewValuesReverse);
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
create(Body);
|
|
|
|
|
2015-08-20 00:04:35 +08:00
|
|
|
Annotator.resetAlternativeAliasBases();
|
2015-04-27 20:32:24 +08:00
|
|
|
// Restore the original values.
|
|
|
|
ValueMap = ValueMapCopy;
|
|
|
|
IDToValue = IDToValueCopy;
|
|
|
|
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&*AfterLoop);
|
2015-04-27 20:32:24 +08:00
|
|
|
removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
|
|
|
|
|
|
|
|
for (const Loop *L : Loops)
|
|
|
|
OutsideLoopIterations.erase(L);
|
|
|
|
|
|
|
|
isl_ast_node_free(For);
|
|
|
|
isl_ast_expr_free(Iterator);
|
|
|
|
isl_id_free(IteratorID);
|
2017-08-23 21:50:30 +08:00
|
|
|
|
|
|
|
ParallelLoops++;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2017-05-22 06:46:57 +08:00
|
|
|
/// Return whether any of @p Node's statements contain partial accesses.
|
|
|
|
///
|
|
|
|
/// Partial accesses are not supported by Polly's vector code generator.
|
|
|
|
static bool hasPartialAccesses(__isl_take isl_ast_node *Node) {
|
|
|
|
return isl_ast_node_foreach_descendant_top_down(
|
|
|
|
Node,
|
|
|
|
[](isl_ast_node *Node, void *User) -> isl_bool {
|
|
|
|
if (isl_ast_node_get_type(Node) != isl_ast_node_user)
|
|
|
|
return isl_bool_true;
|
|
|
|
|
2018-04-29 08:28:26 +08:00
|
|
|
isl::ast_expr Expr =
|
|
|
|
isl::manage(isl_ast_node_user_get_expr(Node));
|
|
|
|
isl::ast_expr StmtExpr = Expr.get_op_arg(0);
|
|
|
|
isl::id Id = StmtExpr.get_id();
|
2017-05-22 06:46:57 +08:00
|
|
|
|
|
|
|
ScopStmt *Stmt =
|
2018-04-29 08:28:26 +08:00
|
|
|
static_cast<ScopStmt *>(isl_id_get_user(Id.get()));
|
2017-08-07 00:39:52 +08:00
|
|
|
isl::set StmtDom = Stmt->getDomain();
|
2017-05-22 06:46:57 +08:00
|
|
|
for (auto *MA : *Stmt) {
|
|
|
|
if (MA->isLatestPartialAccess())
|
|
|
|
return isl_bool_error;
|
|
|
|
}
|
|
|
|
return isl_bool_true;
|
|
|
|
},
|
|
|
|
nullptr) == isl_stat_error;
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
|
|
|
|
bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
|
|
|
|
|
|
|
|
if (Vector && IslAstInfo::isInnermostParallel(For) &&
|
|
|
|
!IslAstInfo::isReductionParallel(For)) {
|
2018-08-01 07:01:50 +08:00
|
|
|
int VectorWidth = getNumberOfIterations(isl::manage_copy(For));
|
2017-05-22 06:46:57 +08:00
|
|
|
if (1 < VectorWidth && VectorWidth <= 16 && !hasPartialAccesses(For)) {
|
2015-04-27 20:32:24 +08:00
|
|
|
createForVector(For, VectorWidth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IslAstInfo::isExecutedInParallel(For)) {
|
|
|
|
createForParallel(For);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-30 05:36:34 +08:00
|
|
|
bool Parallel =
|
|
|
|
(IslAstInfo::isParallel(For) && !IslAstInfo::isReductionParallel(For));
|
2018-08-01 06:43:04 +08:00
|
|
|
createForSequential(isl::manage(For), Parallel);
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
|
|
|
|
isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
|
|
|
|
|
|
|
|
Function *F = Builder.GetInsertBlock()->getParent();
|
|
|
|
LLVMContext &Context = F->getContext();
|
|
|
|
|
2015-11-07 06:56:54 +08:00
|
|
|
BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
|
|
|
|
&*Builder.GetInsertPoint(), &DT, &LI);
|
2015-04-27 20:32:24 +08:00
|
|
|
CondBB->setName("polly.cond");
|
2015-11-07 06:56:54 +08:00
|
|
|
BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI);
|
2015-04-27 20:32:24 +08:00
|
|
|
MergeBB->setName("polly.merge");
|
|
|
|
BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
|
|
|
|
BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
|
|
|
|
|
|
|
|
DT.addNewBlock(ThenBB, CondBB);
|
|
|
|
DT.addNewBlock(ElseBB, CondBB);
|
|
|
|
DT.changeImmediateDominator(MergeBB, CondBB);
|
|
|
|
|
|
|
|
Loop *L = LI.getLoopFor(CondBB);
|
|
|
|
if (L) {
|
|
|
|
L->addBasicBlockToLoop(ThenBB, LI);
|
|
|
|
L->addBasicBlockToLoop(ElseBB, LI);
|
|
|
|
}
|
|
|
|
|
|
|
|
CondBB->getTerminator()->eraseFromParent();
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(CondBB);
|
|
|
|
Value *Predicate = ExprBuilder.create(Cond);
|
|
|
|
Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
|
|
|
|
Builder.SetInsertPoint(ThenBB);
|
|
|
|
Builder.CreateBr(MergeBB);
|
|
|
|
Builder.SetInsertPoint(ElseBB);
|
|
|
|
Builder.CreateBr(MergeBB);
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&ThenBB->front());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
create(isl_ast_node_if_get_then(If));
|
|
|
|
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&ElseBB->front());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
if (isl_ast_node_if_has_else(If))
|
|
|
|
create(isl_ast_node_if_get_else(If));
|
|
|
|
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&MergeBB->front());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
|
|
|
isl_ast_node_free(If);
|
2017-08-23 21:50:30 +08:00
|
|
|
|
|
|
|
IfConditions++;
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
__isl_give isl_id_to_ast_expr *
|
|
|
|
IslNodeBuilder::createNewAccesses(ScopStmt *Stmt,
|
2015-09-05 21:03:57 +08:00
|
|
|
__isl_keep isl_ast_node *Node) {
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr *NewAccesses =
|
2017-11-20 06:13:34 +08:00
|
|
|
isl_id_to_ast_expr_alloc(Stmt->getParent()->getIslCtx().get(), 0);
|
2016-02-16 20:11:03 +08:00
|
|
|
|
|
|
|
auto *Build = IslAstInfo::getBuild(Node);
|
|
|
|
assert(Build && "Could not obtain isl_ast_build from user node");
|
2018-02-20 15:26:58 +08:00
|
|
|
Stmt->setAstBuild(isl::manage_copy(Build));
|
2016-02-16 20:11:03 +08:00
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
for (auto *MA : *Stmt) {
|
2016-11-23 04:21:16 +08:00
|
|
|
if (!MA->hasNewAccessRelation()) {
|
|
|
|
if (PollyGenerateExpressions) {
|
|
|
|
if (!MA->isAffine())
|
|
|
|
continue;
|
|
|
|
if (MA->getLatestScopArrayInfo()->getBasePtrOriginSAI())
|
|
|
|
continue;
|
2015-08-27 15:28:16 +08:00
|
|
|
|
2016-11-23 04:21:16 +08:00
|
|
|
auto *BasePtr =
|
|
|
|
dyn_cast<Instruction>(MA->getLatestScopArrayInfo()->getBasePtr());
|
|
|
|
if (BasePtr && Stmt->getParent()->getRegion().contains(BasePtr))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(MA->isAffine() &&
|
|
|
|
"Only affine memory accesses can be code generated");
|
2016-10-01 02:29:37 +08:00
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
auto Schedule = isl_ast_build_get_schedule(Build);
|
2016-11-06 05:46:01 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2017-05-22 06:46:57 +08:00
|
|
|
if (MA->isRead()) {
|
2017-08-07 00:39:52 +08:00
|
|
|
auto Dom = Stmt->getDomain().release();
|
2017-05-22 06:46:57 +08:00
|
|
|
auto SchedDom = isl_set_from_union_set(
|
|
|
|
isl_union_map_domain(isl_union_map_copy(Schedule)));
|
2017-07-23 12:08:38 +08:00
|
|
|
auto AccDom = isl_map_domain(MA->getAccessRelation().release());
|
2017-08-07 04:11:59 +08:00
|
|
|
Dom = isl_set_intersect_params(Dom,
|
|
|
|
Stmt->getParent()->getContext().release());
|
|
|
|
SchedDom = isl_set_intersect_params(
|
|
|
|
SchedDom, Stmt->getParent()->getContext().release());
|
2017-05-22 06:46:57 +08:00
|
|
|
assert(isl_set_is_subset(SchedDom, AccDom) &&
|
|
|
|
"Access relation not defined on full schedule domain");
|
|
|
|
assert(isl_set_is_subset(Dom, AccDom) &&
|
|
|
|
"Access relation not defined on full domain");
|
|
|
|
isl_set_free(AccDom);
|
|
|
|
isl_set_free(SchedDom);
|
|
|
|
isl_set_free(Dom);
|
|
|
|
}
|
2016-11-06 05:46:01 +08:00
|
|
|
#endif
|
|
|
|
|
2017-07-23 12:08:52 +08:00
|
|
|
auto PWAccRel =
|
|
|
|
MA->applyScheduleToAccessRelation(isl::manage(Schedule)).release();
|
2015-08-27 15:28:16 +08:00
|
|
|
|
2017-05-22 06:46:57 +08:00
|
|
|
// isl cannot generate an index expression for access-nothing accesses.
|
|
|
|
isl::set AccDomain =
|
2018-04-29 08:28:26 +08:00
|
|
|
isl::manage(isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(PWAccRel)));
|
2017-11-22 06:11:10 +08:00
|
|
|
isl::set Context = S.getContext();
|
|
|
|
AccDomain = AccDomain.intersect_params(Context);
|
2018-04-29 08:28:26 +08:00
|
|
|
if (AccDomain.is_empty()) {
|
2017-05-22 06:46:57 +08:00
|
|
|
isl_pw_multi_aff_free(PWAccRel);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
auto AccessExpr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
|
2017-07-23 12:08:11 +08:00
|
|
|
NewAccesses =
|
|
|
|
isl_id_to_ast_expr_set(NewAccesses, MA->getId().release(), AccessExpr);
|
2015-08-27 15:28:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewAccesses;
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:16:50 +08:00
|
|
|
void IslNodeBuilder::createSubstitutions(__isl_take isl_ast_expr *Expr,
|
|
|
|
ScopStmt *Stmt, LoopToScevMapT <S) {
|
2015-04-27 20:32:24 +08:00
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expression of type 'op' expected");
|
|
|
|
assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
|
2017-06-08 20:06:15 +08:00
|
|
|
"Operation of type 'call' expected");
|
2015-04-27 20:32:24 +08:00
|
|
|
for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
|
|
|
|
isl_ast_expr *SubExpr;
|
|
|
|
Value *V;
|
|
|
|
|
|
|
|
SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
|
|
|
|
V = ExprBuilder.create(SubExpr);
|
|
|
|
ScalarEvolution *SE = Stmt->getParent()->getSE();
|
|
|
|
LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createSubstitutionsVector(
|
2015-09-05 17:56:54 +08:00
|
|
|
__isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
|
2015-04-27 20:32:24 +08:00
|
|
|
std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
|
|
|
|
__isl_take isl_id *IteratorID) {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
Value *OldValue = IDToValue[IteratorID];
|
|
|
|
for (Value *IV : IVS) {
|
|
|
|
IDToValue[IteratorID] = IV;
|
2015-09-05 17:56:54 +08:00
|
|
|
createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VLTS[i]);
|
2015-04-27 20:32:24 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDToValue[IteratorID] = OldValue;
|
|
|
|
isl_id_free(IteratorID);
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
}
|
|
|
|
|
2016-09-14 14:26:09 +08:00
|
|
|
void IslNodeBuilder::generateCopyStmt(
|
|
|
|
ScopStmt *Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
|
|
|
|
assert(Stmt->size() == 2);
|
|
|
|
auto ReadAccess = Stmt->begin();
|
|
|
|
auto WriteAccess = ReadAccess++;
|
|
|
|
assert((*ReadAccess)->isRead() && (*WriteAccess)->isMustWrite());
|
|
|
|
assert((*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() &&
|
|
|
|
"Accesses use the same data type");
|
|
|
|
assert((*ReadAccess)->isArrayKind() && (*WriteAccess)->isArrayKind());
|
|
|
|
auto *AccessExpr =
|
2017-07-23 12:08:11 +08:00
|
|
|
isl_id_to_ast_expr_get(NewAccesses, (*ReadAccess)->getId().release());
|
2016-09-14 14:26:09 +08:00
|
|
|
auto *LoadValue = ExprBuilder.create(AccessExpr);
|
2017-07-23 12:08:11 +08:00
|
|
|
AccessExpr =
|
|
|
|
isl_id_to_ast_expr_get(NewAccesses, (*WriteAccess)->getId().release());
|
2016-09-14 14:26:09 +08:00
|
|
|
auto *StoreAddr = ExprBuilder.createAccessAddress(AccessExpr);
|
|
|
|
Builder.CreateStore(LoadValue, StoreAddr);
|
|
|
|
}
|
|
|
|
|
2017-08-06 10:07:11 +08:00
|
|
|
Value *IslNodeBuilder::materializeNonScopLoopInductionVariable(const Loop *L) {
|
|
|
|
assert(OutsideLoopIterations.find(L) == OutsideLoopIterations.end() &&
|
|
|
|
"trying to materialize loop induction variable twice");
|
|
|
|
const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
|
|
|
|
SE.getUnknown(Builder.getInt64(1)), L,
|
|
|
|
SCEV::FlagAnyWrap);
|
|
|
|
Value *V = generateSCEV(OuterLIV);
|
|
|
|
OutsideLoopIterations[L] = SE.getUnknown(V);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
|
|
|
|
LoopToScevMapT LTS;
|
|
|
|
isl_id *Id;
|
|
|
|
ScopStmt *Stmt;
|
|
|
|
|
|
|
|
isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
|
|
|
|
isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
Id = isl_ast_expr_get_id(StmtExpr);
|
|
|
|
isl_ast_expr_free(StmtExpr);
|
|
|
|
|
|
|
|
LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
|
|
|
|
|
|
|
|
Stmt = (ScopStmt *)isl_id_get_user(Id);
|
2015-09-05 21:03:57 +08:00
|
|
|
auto *NewAccesses = createNewAccesses(Stmt, User);
|
2016-09-14 14:26:09 +08:00
|
|
|
if (Stmt->isCopyStmt()) {
|
|
|
|
generateCopyStmt(Stmt, NewAccesses);
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
} else {
|
|
|
|
createSubstitutions(Expr, Stmt, LTS);
|
2015-08-27 15:28:16 +08:00
|
|
|
|
2016-09-14 14:26:09 +08:00
|
|
|
if (Stmt->isBlockStmt())
|
|
|
|
BlockGen.copyStmt(*Stmt, LTS, NewAccesses);
|
|
|
|
else
|
|
|
|
RegionGen.copyStmt(*Stmt, LTS, NewAccesses);
|
|
|
|
}
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2015-08-27 15:28:16 +08:00
|
|
|
isl_id_to_ast_expr_free(NewAccesses);
|
2015-04-27 20:32:24 +08:00
|
|
|
isl_ast_node_free(User);
|
|
|
|
isl_id_free(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
|
|
|
|
isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
|
|
|
|
|
|
|
|
for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
|
|
|
|
create(isl_ast_node_list_get_ast_node(List, i));
|
|
|
|
|
|
|
|
isl_ast_node_free(Block);
|
|
|
|
isl_ast_node_list_free(List);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
|
|
|
|
switch (isl_ast_node_get_type(Node)) {
|
|
|
|
case isl_ast_node_error:
|
|
|
|
llvm_unreachable("code generation error");
|
|
|
|
case isl_ast_node_mark:
|
2015-08-23 17:11:00 +08:00
|
|
|
createMark(Node);
|
|
|
|
return;
|
2015-04-27 20:32:24 +08:00
|
|
|
case isl_ast_node_for:
|
|
|
|
createFor(Node);
|
|
|
|
return;
|
|
|
|
case isl_ast_node_if:
|
|
|
|
createIf(Node);
|
|
|
|
return;
|
|
|
|
case isl_ast_node_user:
|
|
|
|
createUser(Node);
|
|
|
|
return;
|
|
|
|
case isl_ast_node_block:
|
|
|
|
createBlock(Node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown isl_ast_node type");
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
bool IslNodeBuilder::materializeValue(isl_id *Id) {
|
2015-09-30 17:52:08 +08:00
|
|
|
// If the Id is already mapped, skip it.
|
2015-10-04 01:20:00 +08:00
|
|
|
if (!IDToValue.count(Id)) {
|
2015-10-18 20:39:19 +08:00
|
|
|
auto *ParamSCEV = (const SCEV *)isl_id_get_user(Id);
|
2015-11-11 12:30:07 +08:00
|
|
|
Value *V = nullptr;
|
2015-10-18 20:39:19 +08:00
|
|
|
|
2017-06-08 20:06:15 +08:00
|
|
|
// Parameters could refer to invariant loads that need to be
|
2015-10-18 20:39:19 +08:00
|
|
|
// preloaded before we can generate code for the parameter. Thus,
|
2017-06-08 20:06:15 +08:00
|
|
|
// check if any value referred to in ParamSCEV is an invariant load
|
2015-10-18 20:39:19 +08:00
|
|
|
// and if so make sure its equivalence class is preloaded.
|
|
|
|
SetVector<Value *> Values;
|
2016-04-08 18:25:58 +08:00
|
|
|
findValues(ParamSCEV, SE, Values);
|
2015-11-12 06:40:51 +08:00
|
|
|
for (auto *Val : Values) {
|
|
|
|
// Check if the value is an instruction in a dead block within the SCoP
|
|
|
|
// and if so do not code generate it.
|
|
|
|
if (auto *Inst = dyn_cast<Instruction>(Val)) {
|
2016-05-23 20:40:48 +08:00
|
|
|
if (S.contains(Inst)) {
|
2015-11-12 06:40:51 +08:00
|
|
|
bool IsDead = true;
|
|
|
|
|
|
|
|
// Check for "undef" loads first, then if there is a statement for
|
|
|
|
// the parent of Inst and lastly if the parent of Inst has an empty
|
|
|
|
// domain. In the first and last case the instruction is dead but if
|
|
|
|
// there is a statement or the domain is not empty Inst is not dead.
|
2016-01-28 01:09:17 +08:00
|
|
|
auto MemInst = MemAccInst::dyn_cast(Inst);
|
|
|
|
auto Address = MemInst ? MemInst.getPointerOperand() : nullptr;
|
2017-02-01 18:12:09 +08:00
|
|
|
if (Address && SE.getUnknown(UndefValue::get(Address->getType())) ==
|
2017-11-22 03:25:26 +08:00
|
|
|
SE.getPointerBase(SE.getSCEV(Address))) {
|
2016-02-25 06:08:19 +08:00
|
|
|
} else if (S.getStmtFor(Inst)) {
|
2015-11-12 06:40:51 +08:00
|
|
|
IsDead = false;
|
|
|
|
} else {
|
2017-08-07 05:42:38 +08:00
|
|
|
auto *Domain = S.getDomainConditions(Inst->getParent()).release();
|
2015-11-12 06:40:51 +08:00
|
|
|
IsDead = isl_set_is_empty(Domain);
|
|
|
|
isl_set_free(Domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsDead) {
|
|
|
|
V = UndefValue::get(ParamSCEV->getType());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 20:49:11 +08:00
|
|
|
if (auto *IAClass = S.lookupInvariantEquivClass(Val)) {
|
2015-11-11 12:30:07 +08:00
|
|
|
// Check if this invariant access class is empty, hence if we never
|
|
|
|
// actually added a loads instruction to it. In that case it has no
|
|
|
|
// (meaningful) users and we should not try to code generate it.
|
2016-07-11 20:15:10 +08:00
|
|
|
if (IAClass->InvariantAccesses.empty())
|
2015-11-11 12:30:07 +08:00
|
|
|
V = UndefValue::get(ParamSCEV->getType());
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!preloadInvariantEquivClass(*IAClass)) {
|
|
|
|
isl_id_free(Id);
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 12:30:07 +08:00
|
|
|
}
|
2015-11-12 06:40:51 +08:00
|
|
|
}
|
2015-10-18 20:39:19 +08:00
|
|
|
|
2015-11-12 06:40:51 +08:00
|
|
|
V = V ? V : generateSCEV(ParamSCEV);
|
2015-10-04 01:20:00 +08:00
|
|
|
IDToValue[Id] = V;
|
|
|
|
}
|
2015-09-30 17:52:08 +08:00
|
|
|
|
|
|
|
isl_id_free(Id);
|
2015-11-08 03:46:04 +08:00
|
|
|
return true;
|
2015-09-30 17:52:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-19 07:12:49 +08:00
|
|
|
bool IslNodeBuilder::materializeParameters(isl_set *Set) {
|
2015-09-30 17:52:08 +08:00
|
|
|
for (unsigned i = 0, e = isl_set_dim(Set, isl_dim_param); i < e; ++i) {
|
2017-03-19 07:12:49 +08:00
|
|
|
if (!isl_set_involves_dims(Set, isl_dim_param, i, 1))
|
2015-09-30 17:52:08 +08:00
|
|
|
continue;
|
|
|
|
isl_id *Id = isl_set_get_dim_id(Set, isl_dim_param, i);
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!materializeValue(Id))
|
|
|
|
return false;
|
2015-09-30 17:52:08 +08:00
|
|
|
}
|
2015-11-08 03:46:04 +08:00
|
|
|
return true;
|
2015-09-30 17:52:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-19 07:12:49 +08:00
|
|
|
bool IslNodeBuilder::materializeParameters() {
|
|
|
|
for (const SCEV *Param : S.parameters()) {
|
2017-08-07 03:31:27 +08:00
|
|
|
isl_id *Id = S.getIdForParam(Param).release();
|
2017-03-19 07:12:49 +08:00
|
|
|
if (!materializeValue(Id))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:07:45 +08:00
|
|
|
/// Generate the computation of the size of the outermost dimension from the
|
|
|
|
/// Fortran array descriptor (in this case, `@g_arr`). The final `%size`
|
|
|
|
/// contains the size of the array.
|
|
|
|
///
|
|
|
|
/// %arrty = type { i8*, i64, i64, [3 x %desc.dimensionty] }
|
|
|
|
/// %desc.dimensionty = type { i64, i64, i64 }
|
|
|
|
/// @g_arr = global %arrty zeroinitializer, align 32
|
|
|
|
/// ...
|
|
|
|
/// %0 = load i64, i64* getelementptr inbounds
|
|
|
|
/// (%arrty, %arrty* @g_arr, i64 0, i32 3, i64 0, i32 2)
|
|
|
|
/// %1 = load i64, i64* getelementptr inbounds
|
|
|
|
/// (%arrty, %arrty* @g_arr, i64 0, i32 3, i64 0, i32 1)
|
|
|
|
/// %2 = sub nsw i64 %0, %1
|
|
|
|
/// %size = add nsw i64 %2, 1
|
|
|
|
static Value *buildFADOutermostDimensionLoad(Value *GlobalDescriptor,
|
|
|
|
PollyIRBuilder &Builder,
|
|
|
|
std::string ArrayName) {
|
|
|
|
assert(GlobalDescriptor && "invalid global descriptor given");
|
|
|
|
|
|
|
|
Value *endIdx[4] = {Builder.getInt64(0), Builder.getInt32(3),
|
|
|
|
Builder.getInt64(0), Builder.getInt32(2)};
|
|
|
|
Value *endPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, endIdx,
|
|
|
|
ArrayName + "_end_ptr");
|
|
|
|
Value *end = Builder.CreateLoad(endPtr, ArrayName + "_end");
|
|
|
|
|
|
|
|
Value *beginIdx[4] = {Builder.getInt64(0), Builder.getInt32(3),
|
|
|
|
Builder.getInt64(0), Builder.getInt32(1)};
|
|
|
|
Value *beginPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, beginIdx,
|
|
|
|
ArrayName + "_begin_ptr");
|
|
|
|
Value *begin = Builder.CreateLoad(beginPtr, ArrayName + "_begin");
|
|
|
|
|
|
|
|
Value *size =
|
|
|
|
Builder.CreateNSWSub(end, begin, ArrayName + "_end_begin_delta");
|
|
|
|
Type *endType = dyn_cast<IntegerType>(end->getType());
|
|
|
|
assert(endType && "expected type of end to be integral");
|
|
|
|
|
|
|
|
size = Builder.CreateNSWAdd(end,
|
|
|
|
ConstantInt::get(endType, 1, /* signed = */ true),
|
|
|
|
ArrayName + "_size");
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IslNodeBuilder::materializeFortranArrayOutermostDimension() {
|
2017-09-02 02:55:43 +08:00
|
|
|
for (ScopArrayInfo *Array : S.arrays()) {
|
|
|
|
if (Array->getNumberOfDimensions() == 0)
|
|
|
|
continue;
|
2017-05-19 23:07:45 +08:00
|
|
|
|
2017-09-02 02:55:43 +08:00
|
|
|
Value *FAD = Array->getFortranArrayDescriptor();
|
|
|
|
if (!FAD)
|
|
|
|
continue;
|
2017-05-19 23:07:45 +08:00
|
|
|
|
2017-09-02 02:55:43 +08:00
|
|
|
isl_pw_aff *ParametricPwAff = Array->getDimensionSizePw(0).release();
|
|
|
|
assert(ParametricPwAff && "parametric pw_aff corresponding "
|
|
|
|
"to outermost dimension does not "
|
|
|
|
"exist");
|
2017-05-19 23:07:45 +08:00
|
|
|
|
2017-09-02 02:55:43 +08:00
|
|
|
isl_id *Id = isl_pw_aff_get_dim_id(ParametricPwAff, isl_dim_param, 0);
|
|
|
|
isl_pw_aff_free(ParametricPwAff);
|
2017-05-19 23:07:45 +08:00
|
|
|
|
2017-09-02 02:55:43 +08:00
|
|
|
assert(Id && "pw_aff is not parametric");
|
2017-05-19 23:07:45 +08:00
|
|
|
|
2017-09-02 02:55:43 +08:00
|
|
|
if (IDToValue.count(Id)) {
|
2017-05-19 23:07:45 +08:00
|
|
|
isl_id_free(Id);
|
2017-09-02 02:55:43 +08:00
|
|
|
continue;
|
2017-05-19 23:07:45 +08:00
|
|
|
}
|
2017-09-02 02:55:43 +08:00
|
|
|
|
|
|
|
Value *FinalValue =
|
|
|
|
buildFADOutermostDimensionLoad(FAD, Builder, Array->getName());
|
|
|
|
assert(FinalValue && "unable to build Fortran array "
|
|
|
|
"descriptor load of outermost dimension");
|
|
|
|
IDToValue[Id] = FinalValue;
|
|
|
|
isl_id_free(Id);
|
2017-05-19 23:07:45 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:17:36 +08:00
|
|
|
Value *IslNodeBuilder::preloadUnconditionally(isl_set *AccessRange,
|
2016-01-19 08:17:21 +08:00
|
|
|
isl_ast_build *Build,
|
|
|
|
Instruction *AccInst) {
|
2015-09-30 07:47:21 +08:00
|
|
|
isl_pw_multi_aff *PWAccRel = isl_pw_multi_aff_from_set(AccessRange);
|
|
|
|
isl_ast_expr *Access =
|
|
|
|
isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
|
2016-02-07 05:23:39 +08:00
|
|
|
auto *Address = isl_ast_expr_address_of(Access);
|
|
|
|
auto *AddressValue = ExprBuilder.create(Address);
|
|
|
|
Value *PreloadVal;
|
2016-01-19 08:17:21 +08:00
|
|
|
|
2015-11-11 14:20:25 +08:00
|
|
|
// Correct the type as the SAI might have a different type than the user
|
|
|
|
// expects, especially if the base pointer is a struct.
|
2016-01-19 08:17:21 +08:00
|
|
|
Type *Ty = AccInst->getType();
|
2016-02-07 05:23:39 +08:00
|
|
|
|
|
|
|
auto *Ptr = AddressValue;
|
|
|
|
auto Name = Ptr->getName();
|
2017-04-27 14:42:14 +08:00
|
|
|
auto AS = Ptr->getType()->getPointerAddressSpace();
|
|
|
|
Ptr = Builder.CreatePointerCast(Ptr, Ty->getPointerTo(AS), Name + ".cast");
|
2016-02-07 05:23:39 +08:00
|
|
|
PreloadVal = Builder.CreateLoad(Ptr, Name + ".load");
|
2016-01-19 08:17:21 +08:00
|
|
|
if (LoadInst *PreloadInst = dyn_cast<LoadInst>(PreloadVal))
|
|
|
|
PreloadInst->setAlignment(dyn_cast<LoadInst>(AccInst)->getAlignment());
|
|
|
|
|
2016-05-23 17:02:54 +08:00
|
|
|
// TODO: This is only a hot fix for SCoP sequences that use the same load
|
|
|
|
// instruction contained and hoisted by one of the SCoPs.
|
|
|
|
if (SE.isSCEVable(Ty))
|
|
|
|
SE.forgetValue(AccInst);
|
|
|
|
|
2015-10-18 20:36:42 +08:00
|
|
|
return PreloadVal;
|
2015-09-30 07:47:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslNodeBuilder::preloadInvariantLoad(const MemoryAccess &MA,
|
2015-10-18 20:36:42 +08:00
|
|
|
isl_set *Domain) {
|
2017-07-23 12:08:45 +08:00
|
|
|
isl_set *AccessRange = isl_map_range(MA.getAddressFunction().release());
|
2017-08-07 03:52:38 +08:00
|
|
|
AccessRange = isl_set_gist_params(AccessRange, S.getContext().release());
|
2016-05-10 19:59:59 +08:00
|
|
|
|
2017-03-19 07:12:49 +08:00
|
|
|
if (!materializeParameters(AccessRange)) {
|
2015-11-08 03:46:04 +08:00
|
|
|
isl_set_free(AccessRange);
|
|
|
|
isl_set_free(Domain);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-08 04:17:36 +08:00
|
|
|
|
2017-08-07 04:11:59 +08:00
|
|
|
auto *Build =
|
|
|
|
isl_ast_build_from_context(isl_set_universe(S.getParamSpace().release()));
|
2015-09-30 07:47:21 +08:00
|
|
|
isl_set *Universe = isl_set_universe(isl_set_get_space(Domain));
|
|
|
|
bool AlwaysExecuted = isl_set_is_equal(Domain, Universe);
|
|
|
|
isl_set_free(Universe);
|
|
|
|
|
2015-10-18 20:36:42 +08:00
|
|
|
Instruction *AccInst = MA.getAccessInstruction();
|
|
|
|
Type *AccInstTy = AccInst->getType();
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
Value *PreloadVal = nullptr;
|
2015-09-30 07:47:21 +08:00
|
|
|
if (AlwaysExecuted) {
|
2016-01-19 08:17:21 +08:00
|
|
|
PreloadVal = preloadUnconditionally(AccessRange, Build, AccInst);
|
2015-11-08 03:46:04 +08:00
|
|
|
isl_ast_build_free(Build);
|
|
|
|
isl_set_free(Domain);
|
|
|
|
return PreloadVal;
|
|
|
|
}
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2017-03-19 07:12:49 +08:00
|
|
|
if (!materializeParameters(Domain)) {
|
2015-11-08 03:46:04 +08:00
|
|
|
isl_ast_build_free(Build);
|
|
|
|
isl_set_free(AccessRange);
|
|
|
|
isl_set_free(Domain);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
isl_ast_expr *DomainCond = isl_ast_build_expr_from_set(Build, Domain);
|
|
|
|
Domain = nullptr;
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2016-05-12 23:12:43 +08:00
|
|
|
ExprBuilder.setTrackOverflow(true);
|
2015-11-08 03:46:04 +08:00
|
|
|
Value *Cond = ExprBuilder.create(DomainCond);
|
2016-05-12 23:12:43 +08:00
|
|
|
Value *OverflowHappened = Builder.CreateNot(ExprBuilder.getOverflowState(),
|
|
|
|
"polly.preload.cond.overflown");
|
|
|
|
Cond = Builder.CreateAnd(Cond, OverflowHappened, "polly.preload.cond.result");
|
|
|
|
ExprBuilder.setTrackOverflow(false);
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!Cond->getType()->isIntegerTy(1))
|
|
|
|
Cond = Builder.CreateIsNotNull(Cond);
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
|
|
|
|
&*Builder.GetInsertPoint(), &DT, &LI);
|
|
|
|
CondBB->setName("polly.preload.cond");
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI);
|
|
|
|
MergeBB->setName("polly.preload.merge");
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
Function *F = Builder.GetInsertBlock()->getParent();
|
|
|
|
LLVMContext &Context = F->getContext();
|
|
|
|
BasicBlock *ExecBB = BasicBlock::Create(Context, "polly.preload.exec", F);
|
|
|
|
|
|
|
|
DT.addNewBlock(ExecBB, CondBB);
|
|
|
|
if (Loop *L = LI.getLoopFor(CondBB))
|
|
|
|
L->addBasicBlockToLoop(ExecBB, LI);
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
auto *CondBBTerminator = CondBB->getTerminator();
|
|
|
|
Builder.SetInsertPoint(CondBBTerminator);
|
|
|
|
Builder.CreateCondBr(Cond, ExecBB, MergeBB);
|
|
|
|
CondBBTerminator->eraseFromParent();
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
Builder.SetInsertPoint(ExecBB);
|
|
|
|
Builder.CreateBr(MergeBB);
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
Builder.SetInsertPoint(ExecBB->getTerminator());
|
2016-01-19 08:17:21 +08:00
|
|
|
Value *PreAccInst = preloadUnconditionally(AccessRange, Build, AccInst);
|
2015-11-08 03:46:04 +08:00
|
|
|
Builder.SetInsertPoint(MergeBB->getTerminator());
|
|
|
|
auto *MergePHI = Builder.CreatePHI(
|
|
|
|
AccInstTy, 2, "polly.preload." + AccInst->getName() + ".merge");
|
2016-03-01 21:05:14 +08:00
|
|
|
PreloadVal = MergePHI;
|
|
|
|
|
|
|
|
if (!PreAccInst) {
|
|
|
|
PreloadVal = nullptr;
|
|
|
|
PreAccInst = UndefValue::get(AccInstTy);
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
MergePHI->addIncoming(PreAccInst, ExecBB);
|
|
|
|
MergePHI->addIncoming(Constant::getNullValue(AccInstTy), CondBB);
|
2015-10-18 20:36:42 +08:00
|
|
|
|
|
|
|
isl_ast_build_free(Build);
|
|
|
|
return PreloadVal;
|
2015-09-30 07:47:21 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
bool IslNodeBuilder::preloadInvariantEquivClass(
|
2016-04-27 20:49:11 +08:00
|
|
|
InvariantEquivClassTy &IAClass) {
|
2015-10-18 20:39:19 +08:00
|
|
|
// For an equivalence class of invariant loads we pre-load the representing
|
|
|
|
// element with the unified execution context. However, we have to map all
|
|
|
|
// elements of the class to the one preloaded load as they are referenced
|
|
|
|
// during the code generation and therefor need to be mapped.
|
2016-07-11 20:15:10 +08:00
|
|
|
const MemoryAccessList &MAs = IAClass.InvariantAccesses;
|
2015-11-11 12:30:07 +08:00
|
|
|
if (MAs.empty())
|
|
|
|
return true;
|
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
MemoryAccess *MA = MAs.front();
|
ScopInfo: Harmonize the different array kinds
Over time different vocabulary has been introduced to describe the different
memory objects in Polly, resulting in different - often inconsistent - naming
schemes in different parts of Polly. We now standartize this to the following
scheme:
KindArray, KindValue, KindPHI, KindExitPHI
| ------- isScalar -----------|
In most cases this naming scheme has already been used previously (this
minimizes changes and ensures we remain consistent with previous publications).
The main change is that we remove KindScalar to clearify the difference between
a scalar as a memory object of kind Value, PHI or ExitPHI and a value (former
KindScalar) which is a memory object modeling a llvm::Value.
We also move all documentation to the Kind* enum in the ScopArrayInfo class,
remove the second enum in the MemoryAccess class and update documentation to be
formulated from the perspective of the memory object, rather than the memory
access. The terms "Implicit"/"Explicit", formerly used to describe memory
accesses, have been dropped. From the perspective of memory accesses they
described the different memory kinds well - especially from the perspective of
code generation - but just from the perspective of a memory object it seems more
straightforward to talk about scalars and arrays, rather than explicit and
implicit arrays. The last comment is clearly subjective, though. A less
subjective reason to go for these terms is the historic use both in mailing list
discussions and publications.
llvm-svn: 255467
2015-12-14 03:59:01 +08:00
|
|
|
assert(MA->isArrayKind() && MA->isRead());
|
2015-10-18 20:39:19 +08:00
|
|
|
|
|
|
|
// If the access function was already mapped, the preload of this equivalence
|
|
|
|
// class was triggered earlier already and doesn't need to be done again.
|
|
|
|
if (ValueMap.count(MA->getAccessInstruction()))
|
2015-11-08 03:46:04 +08:00
|
|
|
return true;
|
|
|
|
|
2016-08-03 13:28:09 +08:00
|
|
|
// Check for recursion which can be caused by additional constraints, e.g.,
|
|
|
|
// non-finite loop constraints. In such a case we have to bail out and insert
|
2015-11-08 03:46:04 +08:00
|
|
|
// a "false" runtime check that will cause the original code to be executed.
|
2016-07-11 20:27:04 +08:00
|
|
|
auto PtrId = std::make_pair(IAClass.IdentifyingPointer, IAClass.AccessType);
|
2016-02-08 01:30:13 +08:00
|
|
|
if (!PreloadedPtrs.insert(PtrId).second)
|
2015-11-08 03:46:04 +08:00
|
|
|
return false;
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2016-08-03 13:28:09 +08:00
|
|
|
// The execution context of the IAClass.
|
2017-11-20 06:13:34 +08:00
|
|
|
isl::set &ExecutionCtx = IAClass.ExecutionContext;
|
2016-04-27 20:49:11 +08:00
|
|
|
|
2015-11-04 00:49:02 +08:00
|
|
|
// If the base pointer of this class is dependent on another one we have to
|
|
|
|
// make sure it was preloaded already.
|
2016-02-07 21:57:32 +08:00
|
|
|
auto *SAI = MA->getScopArrayInfo();
|
2016-04-27 20:49:11 +08:00
|
|
|
if (auto *BaseIAClass = S.lookupInvariantEquivClass(SAI->getBasePtr())) {
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!preloadInvariantEquivClass(*BaseIAClass))
|
|
|
|
return false;
|
2015-11-04 00:49:02 +08:00
|
|
|
|
2016-04-27 20:49:11 +08:00
|
|
|
// After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx and
|
|
|
|
// we need to refine the ExecutionCtx.
|
2017-11-20 06:13:34 +08:00
|
|
|
isl::set BaseExecutionCtx = BaseIAClass->ExecutionContext;
|
|
|
|
ExecutionCtx = ExecutionCtx.intersect(BaseExecutionCtx);
|
2016-04-27 20:49:11 +08:00
|
|
|
}
|
|
|
|
|
2016-10-18 05:04:26 +08:00
|
|
|
// If the size of a dimension is dependent on another class, make sure it is
|
|
|
|
// preloaded.
|
|
|
|
for (unsigned i = 1, e = SAI->getNumberOfDimensions(); i < e; ++i) {
|
|
|
|
const SCEV *Dim = SAI->getDimensionSize(i);
|
|
|
|
SetVector<Value *> Values;
|
|
|
|
findValues(Dim, SE, Values);
|
|
|
|
for (auto *Val : Values) {
|
|
|
|
if (auto *BaseIAClass = S.lookupInvariantEquivClass(Val)) {
|
|
|
|
if (!preloadInvariantEquivClass(*BaseIAClass))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx
|
|
|
|
// and we need to refine the ExecutionCtx.
|
2017-11-20 06:13:34 +08:00
|
|
|
isl::set BaseExecutionCtx = BaseIAClass->ExecutionContext;
|
|
|
|
ExecutionCtx = ExecutionCtx.intersect(BaseExecutionCtx);
|
2016-10-18 05:04:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
Instruction *AccInst = MA->getAccessInstruction();
|
|
|
|
Type *AccInstTy = AccInst->getType();
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2017-11-20 06:13:34 +08:00
|
|
|
Value *PreloadVal = preloadInvariantLoad(*MA, ExecutionCtx.copy());
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!PreloadVal)
|
|
|
|
return false;
|
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
for (const MemoryAccess *MA : MAs) {
|
|
|
|
Instruction *MAAccInst = MA->getAccessInstruction();
|
2016-02-08 01:30:13 +08:00
|
|
|
assert(PreloadVal->getType() == MAAccInst->getType());
|
|
|
|
ValueMap[MAAccInst] = PreloadVal;
|
2015-10-18 20:39:19 +08:00
|
|
|
}
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
if (SE.isSCEVable(AccInstTy)) {
|
2017-08-07 03:31:27 +08:00
|
|
|
isl_id *ParamId = S.getIdForParam(SE.getSCEV(AccInst)).release();
|
2015-10-18 20:39:19 +08:00
|
|
|
if (ParamId)
|
|
|
|
IDToValue[ParamId] = PreloadVal;
|
|
|
|
isl_id_free(ParamId);
|
|
|
|
}
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-11-09 14:28:45 +08:00
|
|
|
BasicBlock *EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock();
|
2017-04-11 08:12:58 +08:00
|
|
|
auto *Alloca = new AllocaInst(AccInstTy, DL.getAllocaAddrSpace(),
|
|
|
|
AccInst->getName() + ".preload.s2a");
|
2015-11-09 14:28:45 +08:00
|
|
|
Alloca->insertBefore(&*EntryBB->getFirstInsertionPt());
|
|
|
|
Builder.CreateStore(PreloadVal, Alloca);
|
2017-03-22 21:57:53 +08:00
|
|
|
ValueMapT PreloadedPointer;
|
|
|
|
PreloadedPointer[PreloadVal] = AccInst;
|
|
|
|
Annotator.addAlternativeAliasBases(PreloadedPointer);
|
2015-11-09 14:28:45 +08:00
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
for (auto *DerivedSAI : SAI->getDerivedSAIs()) {
|
|
|
|
Value *BasePtr = DerivedSAI->getBasePtr();
|
2015-11-03 09:42:59 +08:00
|
|
|
|
|
|
|
for (const MemoryAccess *MA : MAs) {
|
2015-11-09 14:28:45 +08:00
|
|
|
// As the derived SAI information is quite coarse, any load from the
|
|
|
|
// current SAI could be the base pointer of the derived SAI, however we
|
|
|
|
// should only change the base pointer of the derived SAI if we actually
|
|
|
|
// preloaded it.
|
2017-05-10 18:59:58 +08:00
|
|
|
if (BasePtr == MA->getOriginalBaseAddr()) {
|
2016-02-08 01:30:13 +08:00
|
|
|
assert(BasePtr->getType() == PreloadVal->getType());
|
|
|
|
DerivedSAI->setBasePtr(PreloadVal);
|
2015-11-09 14:28:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For scalar derived SAIs we remap the alloca used for the derived value.
|
[Polly] [BlockGenerator] Unify ScalarMap and PhiOpsMap
Instead of keeping two separate maps from Value to Allocas, one for
MemoryType::Value and the other for MemoryType::PHI, we introduce a single map
from ScopArrayInfo to the corresponding Alloca. This change is intended, both as
a general simplification and cleanup, but also to reduce our use of
MemoryAccess::getBaseAddr(). Moving away from using getBaseAddr() makes sure
we have only a single place where the array (and its base pointer) for which we
generate code for is specified, which means we can more easily introduce new
access functions that use a different ScopArrayInfo as base. We already today
experiment with modifiable access functions, so this change does not address
a specific bug, but it just reduces the scope one needs to reason about.
Another motivation for this patch is https://reviews.llvm.org/D28518, where
memory accesses with different base pointers could possibly be mapped to a
single ScopArrayInfo object. Such a mapping is currently not possible, as we
currently generate alloca instructions according to the base addresses of the
memory accesses, not according to the ScopArrayInfo object they belong to. By
making allocas ScopArrayInfo specific, a mapping to a single ScopArrayInfo
object will automatically mean that the same stack slot is used for these
arrays. For D28518 this is not a problem, as only MemoryType::Array objects are
mapping, but resolving this inconsistency will hopefully avoid confusion.
llvm-svn: 293374
2017-01-28 15:42:10 +08:00
|
|
|
if (BasePtr == MA->getAccessInstruction())
|
|
|
|
ScalarMap[DerivedSAI] = Alloca;
|
2015-11-03 09:42:59 +08:00
|
|
|
}
|
2015-10-18 20:39:19 +08:00
|
|
|
}
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
for (const MemoryAccess *MA : MAs) {
|
|
|
|
Instruction *MAAccInst = MA->getAccessInstruction();
|
|
|
|
// Use the escape system to get the correct value to users outside the SCoP.
|
2015-09-30 17:43:20 +08:00
|
|
|
BlockGenerator::EscapeUserVectorTy EscapeUsers;
|
2015-10-18 20:39:19 +08:00
|
|
|
for (auto *U : MAAccInst->users())
|
2015-09-30 07:47:21 +08:00
|
|
|
if (Instruction *UI = dyn_cast<Instruction>(U))
|
2016-05-23 20:40:48 +08:00
|
|
|
if (!S.contains(UI))
|
2015-09-30 17:43:20 +08:00
|
|
|
EscapeUsers.push_back(UI);
|
2015-09-30 07:47:21 +08:00
|
|
|
|
2015-09-30 17:43:20 +08:00
|
|
|
if (EscapeUsers.empty())
|
|
|
|
continue;
|
|
|
|
|
2015-10-18 20:39:19 +08:00
|
|
|
EscapeMap[MA->getAccessInstruction()] =
|
|
|
|
std::make_pair(Alloca, std::move(EscapeUsers));
|
2015-09-30 07:47:21 +08:00
|
|
|
}
|
2015-11-08 03:46:04 +08:00
|
|
|
|
|
|
|
return true;
|
2015-09-30 07:47:21 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:02:43 +08:00
|
|
|
void IslNodeBuilder::allocateNewArrays(BBPair StartExitBlocks) {
|
2016-07-30 17:25:51 +08:00
|
|
|
for (auto &SAI : S.arrays()) {
|
|
|
|
if (SAI->getBasePtr())
|
|
|
|
continue;
|
|
|
|
|
2016-09-13 01:08:31 +08:00
|
|
|
assert(SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) &&
|
|
|
|
"The size of the outermost dimension is used to declare newly "
|
|
|
|
"created arrays that require memory allocation.");
|
|
|
|
|
2016-07-30 17:25:51 +08:00
|
|
|
Type *NewArrayType = nullptr;
|
2017-06-28 21:02:43 +08:00
|
|
|
|
|
|
|
// Get the size of the array = size(dim_1)*...*size(dim_n)
|
|
|
|
uint64_t ArraySizeInt = 1;
|
2016-09-13 01:08:31 +08:00
|
|
|
for (int i = SAI->getNumberOfDimensions() - 1; i >= 0; i--) {
|
2016-07-30 17:25:51 +08:00
|
|
|
auto *DimSize = SAI->getDimensionSize(i);
|
|
|
|
unsigned UnsignedDimSize = static_cast<const SCEVConstant *>(DimSize)
|
|
|
|
->getAPInt()
|
|
|
|
.getLimitedValue();
|
|
|
|
|
|
|
|
if (!NewArrayType)
|
|
|
|
NewArrayType = SAI->getElementType();
|
|
|
|
|
|
|
|
NewArrayType = ArrayType::get(NewArrayType, UnsignedDimSize);
|
2017-06-28 21:02:43 +08:00
|
|
|
ArraySizeInt *= UnsignedDimSize;
|
2016-07-30 17:25:51 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:02:43 +08:00
|
|
|
if (SAI->isOnHeap()) {
|
|
|
|
LLVMContext &Ctx = NewArrayType->getContext();
|
|
|
|
|
|
|
|
// Get the IntPtrTy from the Datalayout
|
|
|
|
auto IntPtrTy = DL.getIntPtrType(Ctx);
|
|
|
|
|
|
|
|
// Get the size of the element type in bits
|
|
|
|
unsigned Size = SAI->getElemSizeInBytes();
|
|
|
|
|
|
|
|
// Insert the malloc call at polly.start
|
|
|
|
auto InstIt = std::get<0>(StartExitBlocks)->getTerminator();
|
|
|
|
auto *CreatedArray = CallInst::CreateMalloc(
|
|
|
|
&*InstIt, IntPtrTy, SAI->getElementType(),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(Ctx), Size),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(Ctx), ArraySizeInt), nullptr,
|
|
|
|
SAI->getName());
|
|
|
|
|
|
|
|
SAI->setBasePtr(CreatedArray);
|
|
|
|
|
|
|
|
// Insert the free call at polly.exiting
|
|
|
|
CallInst::CreateFree(CreatedArray,
|
|
|
|
std::get<1>(StartExitBlocks)->getTerminator());
|
|
|
|
} else {
|
|
|
|
auto InstIt = Builder.GetInsertBlock()
|
|
|
|
->getParent()
|
|
|
|
->getEntryBlock()
|
|
|
|
.getTerminator();
|
|
|
|
|
|
|
|
auto *CreatedArray = new AllocaInst(NewArrayType, DL.getAllocaAddrSpace(),
|
|
|
|
SAI->getName(), &*InstIt);
|
|
|
|
CreatedArray->setAlignment(PollyTargetFirstLevelCacheLineSize);
|
|
|
|
SAI->setBasePtr(CreatedArray);
|
|
|
|
}
|
2016-07-30 17:25:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:46:04 +08:00
|
|
|
bool IslNodeBuilder::preloadInvariantLoads() {
|
2016-04-27 20:49:11 +08:00
|
|
|
auto &InvariantEquivClasses = S.getInvariantAccesses();
|
2015-10-18 20:39:19 +08:00
|
|
|
if (InvariantEquivClasses.empty())
|
2015-11-08 03:46:04 +08:00
|
|
|
return true;
|
2015-10-18 20:39:19 +08:00
|
|
|
|
2015-11-07 06:56:54 +08:00
|
|
|
BasicBlock *PreLoadBB = SplitBlock(Builder.GetInsertBlock(),
|
|
|
|
&*Builder.GetInsertPoint(), &DT, &LI);
|
2015-10-18 20:39:19 +08:00
|
|
|
PreLoadBB->setName("polly.preload.begin");
|
2015-11-07 06:56:54 +08:00
|
|
|
Builder.SetInsertPoint(&PreLoadBB->front());
|
2015-10-18 20:39:19 +08:00
|
|
|
|
2016-04-27 20:49:11 +08:00
|
|
|
for (auto &IAClass : InvariantEquivClasses)
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!preloadInvariantEquivClass(IAClass))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-10-18 20:39:19 +08:00
|
|
|
}
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
|
2015-09-30 17:52:08 +08:00
|
|
|
// Materialize values for the parameters of the SCoP.
|
2017-03-19 07:12:49 +08:00
|
|
|
materializeParameters();
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2017-05-19 23:07:45 +08:00
|
|
|
// materialize the outermost dimension parameters for a Fortran array.
|
|
|
|
// NOTE: materializeParameters() does not work since it looks through
|
|
|
|
// the SCEVs. We don't have a corresponding SCEV for the array size
|
|
|
|
// parameter
|
|
|
|
materializeFortranArrayOutermostDimension();
|
|
|
|
|
2015-04-27 20:32:24 +08:00
|
|
|
// Generate values for the current loop iteration for all surrounding loops.
|
|
|
|
//
|
|
|
|
// We may also reference loops outside of the scop which do not contain the
|
|
|
|
// scop itself, but as the number of such scops may be arbitrarily large we do
|
|
|
|
// not generate code for them here, but only at the point of code generation
|
|
|
|
// where these values are needed.
|
2016-05-23 20:42:38 +08:00
|
|
|
Loop *L = LI.getLoopFor(S.getEntry());
|
2015-04-27 20:32:24 +08:00
|
|
|
|
2016-05-23 20:40:48 +08:00
|
|
|
while (L != nullptr && S.contains(L))
|
2015-04-27 20:32:24 +08:00
|
|
|
L = L->getParentLoop();
|
|
|
|
|
|
|
|
while (L != nullptr) {
|
2017-08-06 10:07:11 +08:00
|
|
|
materializeNonScopLoopInductionVariable(L);
|
2015-04-27 20:32:24 +08:00
|
|
|
L = L->getParentLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_set_free(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
|
2016-08-08 23:25:46 +08:00
|
|
|
/// We pass the insert location of our Builder, as Polly ensures during IR
|
|
|
|
/// generation that there is always a valid CFG into which instructions are
|
|
|
|
/// inserted. As a result, the insertpoint is known to be always followed by a
|
|
|
|
/// terminator instruction. This means the insert point may be specified by a
|
|
|
|
/// terminator instruction, but it can never point to an ->end() iterator
|
|
|
|
/// which does not have a corresponding instruction. Hence, dereferencing
|
|
|
|
/// the insertpoint to obtain an instruction is known to be save.
|
|
|
|
///
|
|
|
|
/// We also do not need to update the Builder here, as new instructions are
|
|
|
|
/// always inserted _before_ the given InsertLocation. As a result, the
|
|
|
|
/// insert location remains valid.
|
2016-08-09 01:35:52 +08:00
|
|
|
assert(Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() &&
|
2016-08-08 23:25:46 +08:00
|
|
|
"Insert location points after last valid instruction");
|
|
|
|
Instruction *InsertLocation = &*Builder.GetInsertPoint();
|
2015-08-18 19:56:00 +08:00
|
|
|
return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(),
|
2016-11-03 06:32:23 +08:00
|
|
|
InsertLocation, &ValueMap,
|
|
|
|
StartBlock->getSinglePredecessor());
|
2015-04-27 20:32:24 +08:00
|
|
|
}
|
2016-08-08 23:41:52 +08:00
|
|
|
|
|
|
|
/// The AST expression we generate to perform the run-time check assumes
|
|
|
|
/// computations on integer types of infinite size. As we only use 64-bit
|
|
|
|
/// arithmetic we check for overflows, in case of which we set the result
|
2017-06-08 20:06:15 +08:00
|
|
|
/// of this run-time check to false to be conservatively correct,
|
2016-08-08 23:41:52 +08:00
|
|
|
Value *IslNodeBuilder::createRTC(isl_ast_expr *Condition) {
|
|
|
|
auto ExprBuilder = getExprBuilder();
|
2017-09-23 23:32:07 +08:00
|
|
|
|
|
|
|
// In case the AST expression has integers larger than 64 bit, bail out. The
|
|
|
|
// resulting LLVM-IR will contain operations on types that use more than 64
|
|
|
|
// bits. These are -- in case wrapping intrinsics are used -- translated to
|
|
|
|
// runtime library calls that are not available on all systems (e.g., Android)
|
|
|
|
// and consequently will result in linker errors.
|
2018-02-20 15:26:58 +08:00
|
|
|
if (ExprBuilder.hasLargeInts(isl::manage_copy(Condition))) {
|
2017-09-23 23:32:07 +08:00
|
|
|
isl_ast_expr_free(Condition);
|
|
|
|
return Builder.getFalse();
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:41:52 +08:00
|
|
|
ExprBuilder.setTrackOverflow(true);
|
|
|
|
Value *RTC = ExprBuilder.create(Condition);
|
|
|
|
if (!RTC->getType()->isIntegerTy(1))
|
|
|
|
RTC = Builder.CreateIsNotNull(RTC);
|
|
|
|
Value *OverflowHappened =
|
|
|
|
Builder.CreateNot(ExprBuilder.getOverflowState(), "polly.rtc.overflown");
|
2016-11-18 05:49:19 +08:00
|
|
|
|
|
|
|
if (PollyGenerateRTCPrint) {
|
|
|
|
auto *F = Builder.GetInsertBlock()->getParent();
|
2016-12-01 16:08:47 +08:00
|
|
|
RuntimeDebugBuilder::createCPUPrinter(
|
|
|
|
Builder,
|
|
|
|
"F: " + F->getName().str() + " R: " + S.getRegion().getNameStr() +
|
2017-08-20 00:26:39 +08:00
|
|
|
"RTC: ",
|
|
|
|
RTC, " Overflow: ", OverflowHappened,
|
|
|
|
"\n"
|
|
|
|
" (0 failed, -1 succeeded)\n"
|
|
|
|
" (if one or both are 0 falling back to original code, if both are -1 "
|
|
|
|
"executing Polly code)\n");
|
2016-11-18 05:49:19 +08:00
|
|
|
}
|
|
|
|
|
2016-08-08 23:41:52 +08:00
|
|
|
RTC = Builder.CreateAnd(RTC, OverflowHappened, "polly.rtc.result");
|
|
|
|
ExprBuilder.setTrackOverflow(false);
|
2016-11-18 05:55:43 +08:00
|
|
|
|
|
|
|
if (!isa<ConstantInt>(RTC))
|
2016-11-18 22:37:08 +08:00
|
|
|
VersionedScops++;
|
2016-11-18 05:55:43 +08:00
|
|
|
|
2016-08-08 23:41:52 +08:00
|
|
|
return RTC;
|
|
|
|
}
|