Skip to content

Commit

Permalink
Consolidate array data pointer loads
Browse files Browse the repository at this point in the history
  • Loading branch information
Prem Chintalapudi committed Jan 5, 2022
1 parent a351c5e commit 85f7d42
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
// merging the `alloca` for the unboxed data and the `alloca` created by the `alloc_opt`
// pass.
PM->add(createArrayOptPass());
//ArrayOpt needs DCE to run after it to eliminate RAUW-ed loads/cmps
PM->add(createDeadCodeEliminationPass());
PM->add(createAllocOptPass());
// consider AggressiveInstCombinePass at optlevel > 2
PM->add(createInstructionCombiningPass());
Expand Down Expand Up @@ -742,6 +744,7 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,

// Run our own SROA on heap objects before LLVM's
PM->add(createArrayOptPass());
PM->add(createDeadCodeEliminationPass());
PM->add(createAllocOptPass());
// Re-run SROA after loop-unrolling (useful for small loops that operate,
// over the structure of an aggregate)
Expand Down
59 changes: 39 additions & 20 deletions src/llvm-array-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace {
struct ArrayAllocation {
CallInst *allocation;
int dimcount;
bool escaped;
};

struct ArrayOpt;
Expand All @@ -44,7 +45,7 @@ namespace {
Optimizer(ArrayOpt *pass) : pass(pass) {}

void initializeAllocations(Function &F);
bool propagate1DArrayLengths();
bool elideArrayLoads();
bool optimizeCmpInsts();

void reset() {
Expand All @@ -61,10 +62,10 @@ namespace {
}
bool runOnFunction(Function &F) override {
optimizer.initializeAllocations(F);
bool propagation_changed = optimizer.propagate1DArrayLengths();
bool elided = optimizer.elideArrayLoads();
//Propagating 1D array allocations may allow more cmp insts to be folded
bool cmp_folded = optimizer.optimizeCmpInsts();
return propagation_changed || cmp_folded;
return elided || cmp_folded;
}
void getAnalysisUsage(AnalysisUsage &AU) const override
{
Expand All @@ -84,7 +85,7 @@ namespace {
if (auto call = dyn_cast<CallInst>(&I)) {
if (jl_alloc::getArrayAllocInfo(info, call) && info.array.dimcount) {
std::size_t idx = array_allocations.size();
array_allocations.push_back({call, info.array.dimcount});
array_allocations.push_back({call, info.array.dimcount, true});
for (int i = 0; i < info.array.dimcount; i++) {
lengths[call->getArgOperand(i + 1)].insert(idx);
}
Expand All @@ -94,22 +95,22 @@ namespace {
}
}

bool Optimizer::propagate1DArrayLengths() {
bool Optimizer::elideArrayLoads() {
bool changed = false;
jl_alloc::AllocUseInfo use_info;
jl_alloc::CheckInst::Stack check_stack;
for (auto &allocation : array_allocations) {
jl_alloc::EscapeAnalysisRequiredArgs args{use_info, check_stack, *pass, *DL};
jl_alloc::runEscapeAnalysis(allocation.allocation, args);
if (allocation.dimcount == 1) {
jl_alloc::EscapeAnalysisRequiredArgs args{use_info, check_stack, *pass, *DL};
jl_alloc::runEscapeAnalysis(allocation.allocation, args);
if (use_info.escaped) {
continue;
}
//If we clobber any of the dimensions,
//we must necessarily clobber the array length
//This allows us to track all clobbers via the
//array length field
#ifdef STORE_ARRAY_LEN
#ifdef STORE_ARRAY_LEN
bool arraylen_clobbered = false;
for (auto &field : use_info.memops) {
for (auto &memop : field.second.accesses) {
Expand All @@ -122,21 +123,39 @@ namespace {
break;
}
}
if (!arraylen_clobbered) {
//The size of the array doesn't change, we can change
//length/dim loads directly to the allocated amount
for (auto &field : use_info.memops) {
for (auto &memop : field.second.accesses) {
if (memop.offset == offsetof(jl_array_t, length)) {
changed = true;
IRBuilder<> builder(memop.inst);
memop.inst->replaceAllUsesWith(builder.CreateIntCast(allocation.allocation->getArgOperand(1), memop.inst->getType(), false, "arraylen"));
memop.inst->eraseFromParent();
}
if (arraylen_clobbered) {
continue;
}
//The size of the array doesn't change, we can change
//length/dim loads directly to the allocated amount
for (auto &field : use_info.memops) {
for (auto &memop : field.second.accesses) {
if (memop.offset == offsetof(jl_array_t, length)) {
changed = true;
IRBuilder<> builder(memop.inst);
memop.inst->replaceAllUsesWith(builder.CreateIntCast(allocation.allocation->getArgOperand(1), memop.inst->getType(), false, "arraylen"));
memop.inst->eraseFromParent();
}
}
}
#endif
allocation.escaped = false;
#else
continue;
#endif
}
//At this point, either this is a multidimensional array
//or it's a 1D array that doesn't escape.
//Either way, we can hoist the data pointer
IRBuilder<> data_builder(allocation.allocation->getNextNode());
auto ppdata = data_builder.CreatePointerCast(allocation.allocation, PointerType::get(PointerType::get(Type::getInt8Ty(data_builder.getContext()), AddressSpace::Loaded), cast<PointerType>(allocation.allocation->getType())->getAddressSpace()));
auto pdata = data_builder.CreateAlignedLoad(ppdata, llvm::Align(sizeof(size_t)), "arraydata");
for (auto &field : use_info.memops) {
for (auto &memop : field.second.accesses) {
if (memop.offset == offsetof(jl_array_t, data)) {
auto data_load = cast<LoadInst>(memop.inst);
data_load->replaceAllUsesWith(data_builder.CreatePointerCast(pdata, data_load->getType(), "arraydata_casted"));
}
}
}
}
return changed;
Expand Down

0 comments on commit 85f7d42

Please sign in to comment.