-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][spirv] Truncate Literal String size at max number words #142916
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
base: main
Are you sure you want to change the base?
[mlir][spirv] Truncate Literal String size at max number words #142916
Conversation
If not truncated the SPIRV serialization would not fail but instead produce an invalid SPIR-V module. Change-Id: I54c4e54d6ad081861b524d4ae236a1e5080b88c4 Signed-off-by: Davide Grohmann <davide.grohmann@arm.com>
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-spirv Author: Davide Grohmann (davidegrohmann) ChangesIf not truncated the SPIRV serialization would not fail but instead produce an invalid SPIR-V module. Full diff: https://github.com/llvm/llvm-project/pull/142916.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h b/mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
index e46a576f1d48e..d3847ae3d3bb2 100644
--- a/mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
+++ b/mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h
@@ -30,6 +30,12 @@ constexpr uint32_t kMagicNumber = 0x07230203;
/// The serializer tool ID registered to the Khronos Group
constexpr uint32_t kGeneratorNumber = 22;
+// Max number of words
+constexpr uint32_t kMaxWordCount = 65535;
+
+// Max number of words for literal
+constexpr uint32_t kMaxLiteralWordCount = kMaxWordCount - 3;
+
/// Appends a SPRI-V module header to `header` with the given `version` and
/// `idBound`.
void appendModuleHeader(SmallVectorImpl<uint32_t> &header,
diff --git a/mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp b/mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
index 31205d8f408f1..4d4d67a012ae1 100644
--- a/mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
+++ b/mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp
@@ -13,6 +13,9 @@
#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
#include "llvm/Config/llvm-config.h" // for LLVM_VERSION_MAJOR
+#include "llvm/Support/Debug.h"
+
+#define DEBUG_TYPE "spirv-binary-utils"
using namespace mlir;
@@ -68,7 +71,18 @@ void spirv::encodeStringLiteralInto(SmallVectorImpl<uint32_t> &binary,
StringRef literal) {
// We need to encode the literal and the null termination.
auto encodingSize = literal.size() / 4 + 1;
+ auto sizeOfDataToCopy = literal.size();
+ if (encodingSize >= kMaxLiteralWordCount) {
+ // reserve one word for the null termination
+ encodingSize = kMaxLiteralWordCount - 1;
+ // do not override the last word (null termination) when copying
+ sizeOfDataToCopy = (encodingSize - 1) * 4;
+ LLVM_DEBUG(llvm::dbgs() << "Truncating string literal to max size ("
+ << std::to_string(kMaxLiteralWordCount - 1)
+ << "): " << literal << "\n");
+ }
auto bufferStartSize = binary.size();
binary.resize(bufferStartSize + encodingSize, 0);
- std::memcpy(binary.data() + bufferStartSize, literal.data(), literal.size());
+ std::memcpy(binary.data() + bufferStartSize, literal.data(),
+ sizeOfDataToCopy);
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!
If possible, could you please try to add a test to exercise this change? I believe a target test should do the trick (see mlir/test/Target/SPIRV
). Those tests serialize given MLIR into SPIR-V and then deserialize the SPIR-V back to MLIR. So, I would hope you could see a truncation after the roundtrip.
It has been tricky to add a good test for this. Something needs to trigger the encoding of a string literal with more than 262140 chars. |
// Max number of words | ||
constexpr uint32_t kMaxWordCount = 65535; | ||
|
||
// Max number of words for literal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Max number of words | |
constexpr uint32_t kMaxWordCount = 65535; | |
// Max number of words for literal | |
/// Max number of words | |
constexpr uint32_t kMaxWordCount = 65535; | |
/// Max number of words for literal |
auto encodingSize = literal.size() / 4 + 1; | ||
auto sizeOfDataToCopy = literal.size(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We shouldn't use auto here: https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable
@@ -68,7 +71,18 @@ void spirv::encodeStringLiteralInto(SmallVectorImpl<uint32_t> &binary, | |||
StringRef literal) { | |||
// We need to encode the literal and the null termination. | |||
auto encodingSize = literal.size() / 4 + 1; | |||
auto sizeOfDataToCopy = literal.size(); | |||
if (encodingSize >= kMaxLiteralWordCount) { | |||
// reserve one word for the null termination |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// reserve one word for the null termination | |
// Reserve one word for the null termination. |
if (encodingSize >= kMaxLiteralWordCount) { | ||
// reserve one word for the null termination | ||
encodingSize = kMaxLiteralWordCount - 1; | ||
// do not override the last word (null termination) when copying |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// do not override the last word (null termination) when copying | |
// Do not override the last word (null termination) when copying. |
// do not override the last word (null termination) when copying | ||
sizeOfDataToCopy = (encodingSize - 1) * 4; | ||
LLVM_DEBUG(llvm::dbgs() << "Truncating string literal to max size (" | ||
<< std::to_string(kMaxLiteralWordCount - 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to call to_string
@@ -30,6 +30,12 @@ constexpr uint32_t kMagicNumber = 0x07230203; | |||
/// The serializer tool ID registered to the Khronos Group | |||
constexpr uint32_t kGeneratorNumber = 22; | |||
|
|||
// Max number of words | |||
constexpr uint32_t kMaxWordCount = 65535; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you either reference the SPIR-V spec or explain why this is the maximum num of words?
I thought that maybe |
If not truncated the SPIRV serialization would not fail but instead produce an invalid SPIR-V module.