You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
multiplications where one operand is a power of 2 and a constant integer, are optimized with a shift operation and the shift amount is calculated using the logBase2 of the constant.
booloptBasicStrengthReduction(Instruction &I) {
auto OpCode = I.getOpcode();
if (OpCode != Instruction::Mul) returnfalse;
Value *Op1 = I.getOperand(0);
Value *Op2 = I.getOperand(1);
ConstantInt *CI = nullptr;
// Check if op is a constant integer and is a power of 2auto isConstPowOf2 = [&CI](Value *op) {
return (CI = dyn_cast<ConstantInt>(op))
and CI->getValue().isPowerOf2()
andnot CI->isOne();
};
if (isConstPowOf2(Op1)) std::swap(Op1, Op2);
if (notisConstPowOf2(Op2)) returnfalse;
errs() << "Triggered train_gpt2 optimization\n";
// Shift amount calculationunsigned ShiftAmount = CI->getValue().logBase2();
// Create a new shift instruction
Instruction *ShiftInst = BinaryOperator::Create(
Instruction::Shl,
Op1, ConstantInt::get(CI->getType(), ShiftAmount)
);
ShiftInst->insertAfter(&I);
I.replaceAllUsesWith(ShiftInst);
returntrue;
}
and we need to add a call to the opt in a runOnBasicBlock function:
boolrunOnBasicBlock(BasicBlock &B) {
bool globallyModified = false;
std::set<Instruction*> toBeErased;
for (auto &I : B) {
bool locallyModified =
// here you can add all your opt passesoptBasicStrengthReduction(I)
|| optExample2(I)
|| optExample3(I)
|| optExample4(I)
...
// dead code eliminationif (locallyModified) {
toBeErased.insert(&I);
globallyModified = true;
}
}
for (auto *I : toBeErased) {
I->eraseFromParent();
}
return globallyModified;
}
to apply the passes we need to convert train_gpt2 to a LLVM-IR using the clang compiler:
I was discussing this yesterday with @jonmasters. Ideally this would be a script that takes llm.c and transforms it into specialized but still legible C code for a particular architecture. It could do buffer size tuning etc like Mojo🔥
It would also be nice to have a memory/cache layout visualizer.
Here is a little example:
multiplications where one operand is a power of 2 and a constant integer, are optimized with a shift operation and the shift amount is calculated using the logBase2 of the constant.
and we need to add a call to the opt in a
runOnBasicBlock
function:to apply the passes we need to convert
train_gpt2
to a LLVM-IR using the clang compiler:The text was updated successfully, but these errors were encountered: