[llvm] [SPIR-V] Add llvm.loop.unroll metadata lowering (PR #132062)

Nathan Gauër via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 21 08:27:05 PDT 2025


================
@@ -611,6 +611,40 @@ class SPIRVStructurizer : public FunctionPass {
       auto MergeAddress = BlockAddress::get(Merge->getParent(), Merge);
       auto ContinueAddress = BlockAddress::get(Continue->getParent(), Continue);
       SmallVector<Value *, 2> Args = {MergeAddress, ContinueAddress};
+      unsigned LC = SPIRV::LoopControl::None;
+      // Currently used only to store PartialCount value. Later when other
+      // LoopControls are added - this map should be sorted before making
+      // them loop_merge operands to satisfy 3.23. Loop Control requirements.
+      std::vector<std::pair<unsigned, unsigned>> MaskToValueMap;
+      if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable")) {
+        LC |= SPIRV::LoopControl::DontUnroll;
+      } else {
+        if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable")) {
+          LC |= SPIRV::LoopControl::Unroll;
+        }
+        std::optional<int> Count =
+            getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
+        if (Count && Count != 1) {
+          LC |= SPIRV::LoopControl::PartialCount;
+          MaskToValueMap.emplace_back(
+              std::make_pair(SPIRV::LoopControl::PartialCount, *Count));
+        }
+        if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full")) {
+          // llvm.loop.unroll.full doesn't have a direct counterpart in SPIR-V,
+          // the closest thing we can do is to add Unroll mask and if the trip
+          // count is not known at compile time - either disable unrolling by
+          // setting PartialCount to 1 or reuse already available PartialCount.
+          LC |= SPIRV::LoopControl::Unroll;
+          if ((LC & SPIRV::LoopControl::PartialCount) == 0) {
+            LC |= SPIRV::LoopControl::PartialCount;
+            MaskToValueMap.emplace_back(
+                std::make_pair(SPIRV::LoopControl::PartialCount, 1));
+          }
----------------
Keenuts wrote:

Not sure I understand:
Why would `unroll.full` imply `unroll.count = 1`?
Isn't `unroll.full` the same as `LoopControl::Unroll`?

https://github.com/llvm/llvm-project/pull/132062


More information about the llvm-commits mailing list