Skip to content

[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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

davidegrohmann
Copy link

If not truncated the SPIRV serialization would not fail but instead produce an invalid SPIR-V module.

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>
Copy link

github-actions bot commented Jun 5, 2025

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 @ followed by their GitHub username.

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.

@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-spirv

Author: Davide Grohmann (davidegrohmann)

Changes

If 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:

  • (modified) mlir/include/mlir/Target/SPIRV/SPIRVBinaryUtils.h (+6)
  • (modified) mlir/lib/Target/SPIRV/SPIRVBinaryUtils.cpp (+15-1)
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);
 }

Copy link
Contributor

@IgWod-IMG IgWod-IMG left a 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.

@davidegrohmann
Copy link
Author

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.
Any suggestion on which spirv operation to use in such a test?

Comment on lines +33 to +36
// Max number of words
constexpr uint32_t kMaxWordCount = 65535;

// Max number of words for literal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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

Comment on lines 73 to +74
auto encodingSize = literal.size() / 4 + 1;
auto sizeOfDataToCopy = literal.size();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// reserve one word for the null termination
// Reserve one word for the null termination.

See https://llvm.org/docs/CodingStandards.html#commenting

if (encodingSize >= kMaxLiteralWordCount) {
// reserve one word for the null termination
encodingSize = kMaxLiteralWordCount - 1;
// do not override the last word (null termination) when copying
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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)
Copy link
Member

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;
Copy link
Member

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?

@IgWod-IMG
Copy link
Contributor

Any suggestion on which spirv operation to use in such a test?

I thought that maybe spirv.EntryPoint would work but no success. I also tried spirv.ExecutionMode. None of my attempts reach your code. So, I’m afraid that I cannot help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants