Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-type extract #2156

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions enzyme/Enzyme/AdjointGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1809,24 +1809,54 @@ class AdjointGenerator : public llvm::InstVisitor<AdjointGenerator> {
SmallVector<Value *, 4> sv;
for (auto i : EVI.getIndices())
sv.push_back(ConstantInt::get(Type::getInt32Ty(EVI.getContext()), i));
size_t size = 1;
size_t storeSize = 1;
if (EVI.getType()->isSized())
size =
storeSize =
(gutils->newFunc->getParent()->getDataLayout().getTypeSizeInBits(
EVI.getType()) +
7) /
8;
for (size_t i = 0; i < gutils->getWidth(); ++i) {
Value *tdiff = (gutils->getWidth() == 1)
? prediff
: gutils->extractMeta(Builder2, prediff, i);
SmallVector<Value *, 4> sv2 = sv;
if (gutils->getWidth() != 1)
sv2.insert(sv2.begin(),
ConstantInt::get(Type::getInt32Ty(EVI.getContext()), i));
((DiffeGradientUtils *)gutils)
->addToDiffe(orig_op0, tdiff, Builder2, TR.addingType(size, &EVI),
sv2);

unsigned start = 0;
auto vd = TR.query(&EVI);

while (1) {
unsigned nextStart = storeSize;

auto dt = vd[{-1}];
for (size_t i = start; i < storeSize; ++i) {
auto nex = vd[{(int)i}];
if ((nex == BaseType::Anything && dt.isFloat()) ||
(dt == BaseType::Anything && nex.isFloat())) {
nextStart = i;
break;
}
bool Legal = true;
dt.checkedOrIn(nex, /*PointerIntSame*/ true, Legal);
if (!Legal) {
nextStart = i;
break;
}
}
unsigned size = nextStart - start;
if (!dt.isKnown()) {

std::string str;
raw_string_ostream ss(str);
ss << "Cannot deduce type of extract " << EVI << vd.str()
<< " start: " << start << " size: " << size
<< " extractSize: " << storeSize;
EmitNoTypeError(str, EVI, gutils, Builder2);
break;
}
if (auto FT = dt.isFloat())
((DiffeGradientUtils *)gutils)
->addToDiffe(orig_op0, prediff, Builder2, FT, start, size, sv,
nullptr, /*ignoreFirstSlicesToDiff*/ sv.size());

if (nextStart == storeSize)
break;
start = nextStart;
}
}

Expand Down
25 changes: 14 additions & 11 deletions enzyme/Enzyme/DiffeGradientUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,10 @@ Value *DiffeGradientUtils::diffe(Value *val, IRBuilder<> &BuilderM) {
return BuilderM.CreateLoad(ty, getDifferential(val));
}

SmallVector<SelectInst *, 4>
DiffeGradientUtils::addToDiffe(Value *val, Value *dif, IRBuilder<> &BuilderM,
Type *addingType, unsigned start, unsigned size,
llvm::ArrayRef<llvm::Value *> idxs,
llvm::Value *mask) {
SmallVector<SelectInst *, 4> DiffeGradientUtils::addToDiffe(
Value *val, Value *dif, IRBuilder<> &BuilderM, Type *addingType,
unsigned start, unsigned size, llvm::ArrayRef<llvm::Value *> idxs,
llvm::Value *mask, size_t ignoreFirstSlicesOfDif) {
assert(addingType);
auto &DL = oldFunc->getParent()->getDataLayout();
Type *VT = val->getType();
Expand All @@ -258,7 +257,7 @@ DiffeGradientUtils::addToDiffe(Value *val, Value *dif, IRBuilder<> &BuilderM,
if (start == 0 && size == storeSize && !isa<StructType>(VT)) {
if (getWidth() == 1) {
SmallVector<unsigned, 1> eidxs;
for (auto idx : idxs) {
for (auto idx : idxs.slice(ignoreFirstSlicesOfDif)) {
eidxs.push_back((unsigned)cast<ConstantInt>(idx)->getZExtValue());
}
return addToDiffe(val, extractMeta(BuilderM, dif, eidxs), BuilderM,
Expand All @@ -270,8 +269,10 @@ DiffeGradientUtils::addToDiffe(Value *val, Value *dif, IRBuilder<> &BuilderM,
SmallVector<unsigned, 1> eidxs = {(unsigned)j};
lidxs.push_back(
ConstantInt::get(Type::getInt32Ty(val->getContext()), j));
for (auto idx : idxs) {
for (auto idx : idxs.slice(ignoreFirstSlicesOfDif)) {
eidxs.push_back((unsigned)cast<ConstantInt>(idx)->getZExtValue());
}
for (auto idx : idxs) {
lidxs.push_back(idx);
}
for (auto v : addToDiffe(val, extractMeta(BuilderM, dif, eidxs),
Expand Down Expand Up @@ -304,8 +305,9 @@ DiffeGradientUtils::addToDiffe(Value *val, Value *dif, IRBuilder<> &BuilderM,
? min(start + size - (unsigned)SL->getElementOffset(i),
(unsigned)subTypeSize)
: subTypeSize;
for (auto v : addToDiffe(val, dif, BuilderM, addingType, sub_start,
sub_end - sub_start, lidxs, mask))
for (auto v :
addToDiffe(val, dif, BuilderM, addingType, sub_start,
sub_end - sub_start, lidxs, mask, ignoreFirstSlicesOfDif))
res.push_back(v);
}
return res;
Expand All @@ -332,8 +334,9 @@ DiffeGradientUtils::addToDiffe(Value *val, Value *dif, IRBuilder<> &BuilderM,
? min(start + size - (unsigned)(i * subTypeSize),
(unsigned)subTypeSize)
: subTypeSize;
for (auto v : addToDiffe(val, dif, BuilderM, addingType, sub_start,
sub_end - sub_start, lidxs, mask))
for (auto v :
addToDiffe(val, dif, BuilderM, addingType, sub_start,
sub_end - sub_start, lidxs, mask, ignoreFirstSlicesOfDif))
res.push_back(v);
}
return res;
Expand Down
2 changes: 1 addition & 1 deletion enzyme/Enzyme/DiffeGradientUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DiffeGradientUtils final : public GradientUtils {
addToDiffe(llvm::Value *val, llvm::Value *dif, llvm::IRBuilder<> &BuilderM,
llvm::Type *addingType, unsigned start, unsigned size,
llvm::ArrayRef<llvm::Value *> idxs = {},
llvm::Value *mask = nullptr);
llvm::Value *mask = nullptr, size_t ignoreFirstSlicesToDiff = 0);

void setDiffe(llvm::Value *val, llvm::Value *toset,
llvm::IRBuilder<> &BuilderM);
Expand Down
Loading