Skip to content

Commit 662aeba

Browse files
Pavel Samolysovbader
andcommitted
[sycl-post-link] Using early exit to make code more readable
Also some local variables were inlined into function calls. Signed-off-by: Pavel Samolysov <[email protected]> Co-authored-by: Alexey Bader <[email protected]>
1 parent 1708dae commit 662aeba

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

llvm/tools/sycl-post-link/SpecConstants.cpp

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -298,30 +298,35 @@ void collectCompositeElementsDefaultValuesRecursive(
298298
size_t NumBytes = M.getDataLayout().getTypeStoreSize(C->getType());
299299
std::fill_n(std::back_inserter(DefaultValues), NumBytes, 0);
300300
Offset += NumBytes;
301-
} else if (auto *DataSeqC = dyn_cast<ConstantDataSequential>(C)) {
301+
return;
302+
}
303+
304+
if (auto *DataSeqC = dyn_cast<ConstantDataSequential>(C)) {
302305
// This code is generic for both vectors and arrays of scalars
303306
for (size_t I = 0; I < DataSeqC->getNumElements(); ++I) {
304307
Constant *El = cast<Constant>(DataSeqC->getElementAsConstant(I));
305308
collectCompositeElementsDefaultValuesRecursive(M, El, Offset,
306309
DefaultValues);
307310
}
308-
} else if (auto *ArrayC = dyn_cast<ConstantArray>(C)) {
311+
return;
312+
}
313+
314+
if (auto *ArrayC = dyn_cast<ConstantArray>(C)) {
309315
// This branch handles arrays of composite types (structs, arrays, etc.)
310-
auto *ArrTy = ArrayC->getType();
311-
for (size_t I = 0; I < ArrTy->getNumElements(); ++I) {
312-
// We are shure the array is not a zeroinitializer.
313-
Constant *El = cast<Constant>(C->getOperand(I));
314-
collectCompositeElementsDefaultValuesRecursive(M, El, Offset,
315-
DefaultValues);
316+
assert(!C->isZeroValue() && "C must not be a zeroinitializer");
317+
for (size_t I = 0; I < ArrayC->getType()->getNumElements(); ++I) {
318+
collectCompositeElementsDefaultValuesRecursive(M, ArrayC->getOperand(I),
319+
Offset, DefaultValues);
316320
}
317-
} else if (auto *StructC = dyn_cast<ConstantStruct>(C)) {
321+
return;
322+
}
323+
324+
if (auto *StructC = dyn_cast<ConstantStruct>(C)) {
325+
assert(!C->isZeroValue() && "C must not be a zeroinitializer");
318326
auto *StructTy = StructC->getType();
319327
const StructLayout *SL = M.getDataLayout().getStructLayout(StructTy);
320328
const size_t BaseDefaultValueOffset = DefaultValues.size();
321329
for (size_t I = 0, E = StructTy->getNumElements(); I < E; ++I) {
322-
// We are shure the structure is not a zeroinitializer.
323-
Constant *El = cast<Constant>(C->getOperand(I));
324-
325330
// When handling elements of a structure, we do not use manually
326331
// calculated offsets (which are sum of sizes of all previously
327332
// encountered elements), but instead rely on data provided for us by
@@ -333,7 +338,8 @@ void collectCompositeElementsDefaultValuesRecursive(
333338
while (LocalOffset != DefaultValues.size())
334339
DefaultValues.push_back(0);
335340

336-
collectCompositeElementsDefaultValuesRecursive(M, El, LocalOffset,
341+
collectCompositeElementsDefaultValuesRecursive(M, StructC->getOperand(I),
342+
LocalOffset,
337343
DefaultValues);
338344
}
339345
const size_t SLSize = SL->getSizeInBytes();
@@ -346,37 +352,39 @@ void collectCompositeElementsDefaultValuesRecursive(
346352
// Update "global" offset according to the total size of a handled struct
347353
// type.
348354
Offset += SLSize;
349-
} else { // Assume that we encountered some scalar element
350-
size_t NumBytes = M.getDataLayout().getTypeStoreSize(C->getType());
351-
if (auto IntConst = dyn_cast<ConstantInt>(C)) {
352-
auto Val = IntConst->getValue().getZExtValue();
353-
std::copy_n(reinterpret_cast<char *>(&Val), NumBytes,
355+
return;
356+
}
357+
358+
// Assume that we encountered some scalar element
359+
size_t NumBytes = M.getDataLayout().getTypeStoreSize(C->getType());
360+
if (auto IntConst = dyn_cast<ConstantInt>(C)) {
361+
auto Val = IntConst->getValue().getZExtValue();
362+
std::copy_n(reinterpret_cast<char *>(&Val), NumBytes,
363+
std::back_inserter(DefaultValues));
364+
} else if (auto FPConst = dyn_cast<ConstantFP>(C)) {
365+
auto Val = FPConst->getValue();
366+
367+
if (NumBytes == 2) {
368+
auto IVal = Val.bitcastToAPInt();
369+
assert(IVal.getBitWidth() == 16);
370+
auto Storage = static_cast<uint16_t>(IVal.getZExtValue());
371+
std::copy_n(reinterpret_cast<char *>(&Storage), NumBytes,
372+
std::back_inserter(DefaultValues));
373+
} else if (NumBytes == 4) {
374+
float v = Val.convertToFloat();
375+
std::copy_n(reinterpret_cast<char *>(&v), NumBytes,
376+
std::back_inserter(DefaultValues));
377+
} else if (NumBytes == 8) {
378+
double v = Val.convertToDouble();
379+
std::copy_n(reinterpret_cast<char *>(&v), NumBytes,
354380
std::back_inserter(DefaultValues));
355-
} else if (auto FPConst = dyn_cast<ConstantFP>(C)) {
356-
auto Val = FPConst->getValue();
357-
358-
if (NumBytes == 2) {
359-
auto IVal = Val.bitcastToAPInt();
360-
assert(IVal.getBitWidth() == 16);
361-
auto Storage = static_cast<uint16_t>(IVal.getZExtValue());
362-
std::copy_n(reinterpret_cast<char *>(&Storage), NumBytes,
363-
std::back_inserter(DefaultValues));
364-
} else if (NumBytes == 4) {
365-
float v = Val.convertToFloat();
366-
std::copy_n(reinterpret_cast<char *>(&v), NumBytes,
367-
std::back_inserter(DefaultValues));
368-
} else if (NumBytes == 8) {
369-
double v = Val.convertToDouble();
370-
std::copy_n(reinterpret_cast<char *>(&v), NumBytes,
371-
std::back_inserter(DefaultValues));
372-
} else {
373-
llvm_unreachable("Unexpected constant floating point type");
374-
}
375381
} else {
376-
llvm_unreachable("Unexpected constant scalar type");
382+
llvm_unreachable("Unexpected constant floating point type");
377383
}
378-
Offset += NumBytes;
384+
} else {
385+
llvm_unreachable("Unexpected constant scalar type");
379386
}
387+
Offset += NumBytes;
380388
}
381389

382390
MDNode *generateSpecConstantMetadata(const Module &M, StringRef SymbolicID,

0 commit comments

Comments
 (0)