3

Clang has loop flattening pass to transform from nested loops:

 for (int i = 0; i < N; ++i)
   for (int j = 0; j < M; ++j)
     f(A[i*M+j]);

into one loop:

 for (int i = 0; i < (N*M); ++i)
   f(A[i]);

However, I cannot find how to enable this loop flattening pass. I try to use -Os -S, however, clang still generates nested loops.

Question: how to see the effect of loop flattening pass? Which options to use?

UPD. I use clang version 12.0.0 on Windows.

pmor
  • 5,392
  • 4
  • 17
  • 36

1 Answers1

2

LoopFlatten has been merged into LLVM since 12.0.0. Make sure you are using the updated version.

Second, it's in the transformation passes, you can't directly compiled with clang options. Use opt to do a transformation pass using LoopFlatten using the options specified in the pass code. If you are not familiar with opt, please refer to here: opt - LLVM optimizer

hddmss
  • 231
  • 1
  • 12
  • Cannot find what is the format of "optimized output file"? Looking at the content of "optimized output file" I see that this is a binary file. However, in which format? Extra: how to obtain assembler code file (like we do with `clang -S`)? – pmor Aug 25 '21 at 10:56
  • Can you please (if possible) point out the source file (and line number), which implements the conditions to trigger the loop flattening? It will be interesting to see the conditions. Example of such conditions (I imagine): "if indexes can be constant-folded AND if indexes are not further used separately (so such indexes can be replaced by auto-incrementing)", and so on. – pmor Aug 25 '21 at 11:10
  • @pmor I'm not sure the opt can't directly output the transformed bc code. But you can definitely get the analysis result from a getAnalysis class, see from here: [LoopInfo Analysis](https://llvm.org/docs/WritingAnLLVMPass.html#the-getanalysis-and-getanalysisifavailable-methods) – hddmss Aug 25 '21 at 19:35