|
13 | 13 | #include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
|
14 | 14 | #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
|
15 | 15 | #include "llvm/Config/llvm-config.h" // for LLVM_VERSION_MAJOR
|
| 16 | +#include "llvm/Support/Debug.h" |
| 17 | + |
| 18 | +#define DEBUG_TYPE "spirv-binary-utils" |
16 | 19 |
|
17 | 20 | using namespace mlir;
|
18 | 21 |
|
@@ -67,8 +70,19 @@ uint32_t spirv::getPrefixedOpcode(uint32_t wordCount, spirv::Opcode opcode) {
|
67 | 70 | void spirv::encodeStringLiteralInto(SmallVectorImpl<uint32_t> &binary,
|
68 | 71 | StringRef literal) {
|
69 | 72 | // We need to encode the literal and the null termination.
|
70 |
| - auto encodingSize = literal.size() / 4 + 1; |
71 |
| - auto bufferStartSize = binary.size(); |
| 73 | + size_t encodingSize = literal.size() / 4 + 1; |
| 74 | + size_t sizeOfDataToCopy = literal.size(); |
| 75 | + if (encodingSize >= kMaxLiteralWordCount) { |
| 76 | + // Reserve one word for the null termination. |
| 77 | + encodingSize = kMaxLiteralWordCount - 1; |
| 78 | + // Do not override the last word (null termination) when copying. |
| 79 | + sizeOfDataToCopy = (encodingSize - 1) * 4; |
| 80 | + LLVM_DEBUG(llvm::dbgs() |
| 81 | + << "Truncating string literal to max size (" |
| 82 | + << (kMaxLiteralWordCount - 1) << "): " << literal << "\n"); |
| 83 | + } |
| 84 | + size_t bufferStartSize = binary.size(); |
72 | 85 | binary.resize(bufferStartSize + encodingSize, 0);
|
73 |
| - std::memcpy(binary.data() + bufferStartSize, literal.data(), literal.size()); |
| 86 | + std::memcpy(binary.data() + bufferStartSize, literal.data(), |
| 87 | + sizeOfDataToCopy); |
74 | 88 | }
|
0 commit comments