Skip to content

Commit

Permalink
Fix for crash on Apple Silicon (#459)
Browse files Browse the repository at this point in the history
* Cherry-pick GC code change by hand

* Fixed signature
  • Loading branch information
Baltoli authored Nov 10, 2021
1 parent b536e97 commit 572cf69
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 70 deletions.
10 changes: 2 additions & 8 deletions include/kllvm/codegen/Decision.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ class Decision {
llvm::LLVMContext &Ctx;
ValueType Cat;
llvm::PHINode *FailSubject, *FailPattern, *FailSort;
llvm::Value *ResultBuffer, *ResultCount, *ResultCapacity;

std::map<var_type, llvm::AllocaInst *> symbols;

Expand All @@ -383,9 +382,7 @@ class Decision {
llvm::BasicBlock *FailureBlock, llvm::IndirectBrInst *FailJump,
llvm::AllocaInst *ChoiceBuffer, llvm::AllocaInst *ChoiceDepth,
llvm::Module *Module, ValueType Cat, llvm::PHINode *FailSubject,
llvm::PHINode *FailPattern, llvm::PHINode *FailSort,
llvm::Value *ResultBuffer, llvm::Value *ResultCount,
llvm::Value *ResultCapacity)
llvm::PHINode *FailPattern, llvm::PHINode *FailSort)
: Definition(Definition)
, CurrentBlock(EntryBlock)
, FailureBlock(FailureBlock)
Expand All @@ -398,10 +395,7 @@ class Decision {
, Cat(Cat)
, FailSubject(FailSubject)
, FailPattern(FailPattern)
, FailSort(FailSort)
, ResultBuffer(ResultBuffer)
, ResultCount(ResultCount)
, ResultCapacity(ResultCapacity) { }
, FailSort(FailSort) { }

/* adds code to the specified basic block to take a single step based on
the specified decision tree and return the result of taking that step. */
Expand Down
53 changes: 9 additions & 44 deletions lib/codegen/Decision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,8 @@ void LeafNode::codegen(Decision *d) {
getOrInsertFunction(
d->Module, "addSearchResult",
llvm::FunctionType::get(
llvm::Type::getVoidTy(d->Ctx),
{type,
llvm::PointerType::getUnqual(
llvm::PointerType::getUnqual(type)),
llvm::Type::getInt64PtrTy(d->Ctx),
llvm::Type::getInt64PtrTy(d->Ctx)},
false)),
{Call, d->ResultBuffer, d->ResultCount, d->ResultCapacity}, "",
d->CurrentBlock);
llvm::Type::getVoidTy(d->Ctx), {type}, false)),
{Call}, "", d->CurrentBlock);
setDebugLoc(Call2);
if (child != FailNode::get()) {
child->codegen(d);
Expand Down Expand Up @@ -647,7 +640,7 @@ void makeEvalOrAnywhereFunction(
int i = 0;
Decision codegen(
definition, block, fail, jump, choiceBuffer, choiceDepth, module,
returnSort, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
returnSort, nullptr, nullptr, nullptr);
for (auto val = matchFunc->arg_begin(); val != matchFunc->arg_end();
++val, ++i) {
val->setName("_" + std::to_string(i + 1));
Expand Down Expand Up @@ -926,16 +919,14 @@ void makeStepFunction(
KOREDefinition *definition, llvm::Module *module, DecisionNode *dt,
bool search) {
auto blockType = getValueType({SortCategory::Symbol, 0}, module);
auto bufType = llvm::PointerType::getUnqual(blockType);
auto debugType
= getDebugType({SortCategory::Symbol, 0}, "SortGeneratedTopCell{}");
llvm::FunctionType *funcType;
std::string name;
if (search) {
name = "stepAll";
funcType = llvm::FunctionType::get(
bufType, {blockType, llvm::Type::getInt64PtrTy(module->getContext())},
false);
llvm::Type::getVoidTy(module->getContext()), {blockType}, false);
} else {
name = "step";
funcType = llvm::FunctionType::get(blockType, {blockType}, false);
Expand All @@ -944,10 +935,7 @@ void makeStepFunction(
resetDebugLoc();
if (search) {
initDebugFunction(
name, name,
getDebugFunctionType(
getPointerDebugType(debugType, "block **"),
{debugType, getPointerDebugType(getLongDebugType(), "uint64_t *")}),
name, name, getDebugFunctionType(getVoidDebugType(), {debugType}),
definition, matchFunc);
} else {
initDebugFunction(
Expand All @@ -968,25 +956,6 @@ void makeStepFunction(
llvm::AllocaInst *choiceBuffer, *choiceDepth;
llvm::IndirectBrInst *jump;

llvm::Value *resultBuffer = nullptr, *resultCount = nullptr,
*resultCapacity = nullptr;
if (search) {
resultBuffer = new llvm::AllocaInst(bufType, 0, "resultBuffer", block);
resultCount = matchFunc->arg_begin() + 1;
resultCapacity = new llvm::AllocaInst(
llvm::Type::getInt64Ty(module->getContext()), 0, "resultCapacity",
block);
llvm::Value *initialBuffer
= allocateTerm(blockType, block, "koreAllocAlwaysGC");
new llvm::StoreInst(initialBuffer, resultBuffer, block);
new llvm::StoreInst(
llvm::ConstantInt::get(llvm::Type::getInt64Ty(module->getContext()), 0),
resultCount, block);
new llvm::StoreInst(
llvm::ConstantInt::get(llvm::Type::getInt64Ty(module->getContext()), 1),
resultCapacity, block);
}

initChoiceBuffer(
dt, module, block, pre_stuck, fail, &choiceBuffer, &choiceDepth, &jump);

Expand All @@ -1000,14 +969,12 @@ void makeStepFunction(
collectedVal->setName("_1");
Decision codegen(
definition, result.second, fail, jump, choiceBuffer, choiceDepth, module,
{SortCategory::Symbol, 0}, nullptr, nullptr, nullptr, resultBuffer,
resultCount, resultCapacity);
{SortCategory::Symbol, 0}, nullptr, nullptr, nullptr);
codegen.store(
std::make_pair(collectedVal->getName().str(), collectedVal->getType()),
collectedVal);
if (search) {
auto result = new llvm::LoadInst(bufType, resultBuffer, "", stuck);
llvm::ReturnInst::Create(module->getContext(), result, stuck);
llvm::ReturnInst::Create(module->getContext(), stuck);
} else {
auto phi
= llvm::PHINode::Create(collectedVal->getType(), 2, "phi_1", stuck);
Expand Down Expand Up @@ -1077,8 +1044,7 @@ void makeMatchReasonFunction(
val->setName("_1");
Decision codegen(
definition, block, fail, jump, choiceBuffer, choiceDepth, module,
{SortCategory::Symbol, 0}, FailSubject, FailPattern, FailSort, nullptr,
nullptr, nullptr);
{SortCategory::Symbol, 0}, FailSubject, FailPattern, FailSort);
codegen.store(std::make_pair(val->getName().str(), val->getType()), val);
llvm::ReturnInst::Create(module->getContext(), stuck);

Expand Down Expand Up @@ -1192,8 +1158,7 @@ void makeStepFunction(
i = 0;
Decision codegen(
definition, header.second, fail, jump, choiceBuffer, choiceDepth, module,
{SortCategory::Symbol, 0}, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr);
{SortCategory::Symbol, 0}, nullptr, nullptr, nullptr);
for (auto val : header.first) {
val->setName(res.residuals[i].occurrence.substr(0, max_name_length));
codegen.store(std::make_pair(val->getName().str(), val->getType()), val);
Expand Down
32 changes: 14 additions & 18 deletions runtime/util/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@
#include "runtime/collect.h"
#include "runtime/header.h"

static std::vector<block *> stepResults;

extern "C" {

void addSearchResult(
block *result, block ***bufPtr, uint64_t *count, uint64_t *capacity) {
if (*count == *capacity) {
size_t len = *count * sizeof(block *) * 2;
block **newBuf = (block **)koreAllocAlwaysGC(len);
memcpy(newBuf, *bufPtr, len);
*bufPtr = newBuf;
*capacity = *capacity * 2;
}
(*bufPtr)[*count] = result;
(*count)++;
void addSearchResult(block *result) {
stepResults.push_back(result);
}

block **take_search_step(block *, uint64_t *);
void take_search_step(block *);
}

static std::list<block *> states;
Expand All @@ -38,6 +31,9 @@ blockEnumerator() {
blocks.push_back(const_cast<block **>(&(keyVal)));
}
blocks.push_back(&state);
for (auto &keyVal : stepResults) {
blocks.push_back(const_cast<block **>(&(keyVal)));
}

return std::make_pair(blocks.begin(), blocks.end());
}
Expand All @@ -60,15 +56,15 @@ take_search_steps(int64_t depth, block *subject) {
states_set.erase(state);
if (depth > 0)
depth--;
uint64_t count;
block **stepResults = take_search_step(state, &count);
if (count == 0) {
stepResults.clear();
take_search_step(state);
if (stepResults.size() == 0) {
results.insert(state);
} else {
for (uint64_t i = 0; i < count; i++) {
auto dirty = states_set.insert(stepResults[i]);
for (block *result : stepResults) {
auto dirty = states_set.insert(result);
if (dirty.second) {
states.push_back(stepResults[i]);
states.push_back(result);
}
}
}
Expand Down

0 comments on commit 572cf69

Please # to comment.