[PATCH] D137991: [NVPTX] Emit pragma nounroll for llvm.loop.unroll.count=1

Dmitry Vassiliev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 14 15:13:19 PST 2022


slydiman created this revision.
slydiman added reviewers: tra, MaskRay.
slydiman added a project: LLVM.
Herald added subscribers: mattd, gchakrabarti, asavonic, StephenFan, zzheng, hiraditya.
Herald added a project: All.
slydiman requested review of this revision.
Herald added subscribers: llvm-commits, jholewinski.

Emit pragma nounroll for llvm.loop.unroll.count=1 (#pragma unroll 1).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137991

Files:
  llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
  llvm/test/CodeGen/NVPTX/nounroll.ll


Index: llvm/test/CodeGen/NVPTX/nounroll.ll
===================================================================
--- llvm/test/CodeGen/NVPTX/nounroll.ll
+++ llvm/test/CodeGen/NVPTX/nounroll.ll
@@ -34,5 +34,37 @@
   ret void
 }
 
+; Compiled from the following CUDA code:
+;
+;   #pragma unroll 1
+;   for (int i = 0; i < 2; ++i)
+;     output[i] = input[i];
+define void @unroll1(float* %input, float* %output) {
+; CHECK-LABEL: .visible .func unroll1(
+entry:
+  br label %for.body
+
+for.body:
+; CHECK: .pragma "nounroll"
+  %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+  %idxprom = sext i32 %i.06 to i64
+  %arrayidx = getelementptr inbounds float, float* %input, i64 %idxprom
+  %0 = load float, float* %arrayidx, align 4
+; CHECK: ld.f32
+  %arrayidx2 = getelementptr inbounds float, float* %output, i64 %idxprom
+  store float %0, float* %arrayidx2, align 4
+; CHECK: st.f32
+  %inc = add nuw nsw i32 %i.06, 1
+  %exitcond = icmp eq i32 %inc, 2
+  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !2
+; CHECK-NOT: ld.f32
+; CHECK-NOT: st.f32
+
+for.end:
+  ret void
+}
+
 !0 = distinct !{!0, !1}
 !1 = !{!"llvm.loop.unroll.disable"}
+!2 = distinct !{!2, !3}
+!3 = !{!"llvm.loop.unroll.count", i32 1}
Index: llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
===================================================================
--- llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -406,8 +406,7 @@
 }
 
 // Return true if MBB is the header of a loop marked with
-// llvm.loop.unroll.disable.
-// TODO: consider "#pragma unroll 1" which is equivalent to "#pragma nounroll".
+// llvm.loop.unroll.disable or llvm.loop.unroll.count=1.
 bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll(
     const MachineBasicBlock &MBB) const {
   MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>();
@@ -428,6 +427,14 @@
               PBB->getTerminator()->getMetadata(LLVMContext::MD_loop)) {
         if (GetUnrollMetadata(LoopID, "llvm.loop.unroll.disable"))
           return true;
+        if (MDNode *UnrollCountMD =
+                GetUnrollMetadata(LoopID, "llvm.loop.unroll.count")) {
+          uint64_t Count =
+              mdconst::extract<ConstantInt>(UnrollCountMD->getOperand(1))
+                  ->getZExtValue();
+          if (Count == 1)
+            return true;
+        }
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137991.475291.patch
Type: text/x-patch
Size: 2354 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221114/faeb66b8/attachment.bin>


More information about the llvm-commits mailing list