[PATCH] D156814: [NFC] Refactor byteswapped writes

Chris Bieneman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 10:27:38 PDT 2023


beanz created this revision.
beanz added reviewers: bogner, bob80905.
Herald added a subscriber: hiraditya.
Herald added a project: All.
beanz requested review of this revision.
Herald added a project: LLVM.

This is an extremely small refactoring to writing byte-swapped values
in the PSV data. I've broken the PPC-BE bots a few times with changes
here because of the fragility of how byte-swapping was being done. This
should make it less likely for me to break BE builders in the future.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156814

Files:
  llvm/lib/MC/DXContainerPSVInfo.cpp


Index: llvm/lib/MC/DXContainerPSVInfo.cpp
===================================================================
--- llvm/lib/MC/DXContainerPSVInfo.cpp
+++ llvm/lib/MC/DXContainerPSVInfo.cpp
@@ -14,6 +14,14 @@
 using namespace llvm::mcdxbc;
 using namespace llvm::dxbc::PSV;
 
+template <typename T>
+std::enable_if_t<std::is_integral<T>::value, void>
+swappingWrite(raw_ostream &OS, T Val) {
+  if (sys::IsBigEndianHost)
+    sys::swapByteOrder(Val);
+  OS.write(reinterpret_cast<const char *>(&Val), sizeof(T));
+}
+
 void PSVRuntimeInfo::write(raw_ostream &OS, uint32_t Version) const {
   uint32_t InfoSize;
   uint32_t BindingSize;
@@ -31,24 +39,15 @@
     InfoSize = sizeof(dxbc::PSV::v2::RuntimeInfo);
     BindingSize = sizeof(dxbc::PSV::v2::ResourceBindInfo);
   }
-  uint32_t InfoSizeSwapped = InfoSize;
-  if (sys::IsBigEndianHost)
-    sys::swapByteOrder(InfoSizeSwapped);
   // Write the size of the info.
-  OS.write(reinterpret_cast<const char *>(&InfoSizeSwapped), sizeof(uint32_t));
+  swappingWrite(OS, InfoSize);
   // Write the info itself.
   OS.write(reinterpret_cast<const char *>(&BaseData), InfoSize);
 
   uint32_t ResourceCount = static_cast<uint32_t>(Resources.size());
-  uint32_t BindingSizeSwapped = BindingSize;
-  if (sys::IsBigEndianHost) {
-    sys::swapByteOrder(ResourceCount);
-    sys::swapByteOrder(BindingSizeSwapped);
-  }
 
-  OS.write(reinterpret_cast<const char *>(&ResourceCount), sizeof(uint32_t));
-  OS.write(reinterpret_cast<const char *>(&BindingSizeSwapped),
-           sizeof(uint32_t));
+  swappingWrite(OS, ResourceCount);
+  swappingWrite(OS, BindingSize);
 
   for (const auto &Res : Resources)
     OS.write(reinterpret_cast<const char *>(&Res), BindingSize);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156814.546123.patch
Type: text/x-patch
Size: 1716 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230801/8a041111/attachment.bin>


More information about the llvm-commits mailing list