[llvm] [OpenMP] [IR Builder] Changes to Support Scan Operation (PR #136035)

Anchu Rajendran S via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 5 12:08:32 PDT 2025


anchuraj wrote:

> > For the above comment, I do not fully understand the design mentioned. I tried to create two lambdas in `ScanInfoInitialize` and stored it as two members (of type lambda) of the class `ScanInfo`, so the flang frontend calls these two lambdas when scan directive is lowered. However, that does not seem to simplify the design.
> 
> ```
> createScanLoop(LoopBodyGenCallbackTy InputPhaseCb, LoopBodyGenCallbackTy ScanPhaseCb, llvm::Value *scanVar, llvm::Type *scanType, ...)
> ```
> 
> It certainly makes the implementation more complicated, the question is by how much.
> 
> The reason why I think this would be preferable is that it does not the entire scan construct is built by a single call. Compared to an outer `createCanonicalScanLoops` where `createScan` has to be called inside the callback. This forces frontends to structure their CodeGen a certain way, and may overlook weird combinations in-between `createCanonicalScanLoops` and `createScan` such as `createCritical`:
> 
> ```
> #pragma omp for reduction(inscan,+:x)
> for (int i = 0; i < n; ++i) {
>   #pragma omp critical
>   { 
>     x += A[i];
>     #pragma omp scan inclusive(x)
>     B[i] = x;
>   }
> }
> ```
> 
> My question is whether you have considered such a design and it seems now you have (and considered not worth the complexity). Given that (IMHO unfortunately) the entire OpenMP builder is already designed to have specific things happen within lambdas. your current design would not be unusual.


- `create scan` adds breaks to structure input and scan phase loops. The insertion point for these modifications is captured at the place where scan directive is encountered.
- `create scan` also adds control flows considering  whether it is inclusive scan or exclusive scan. The information on whether it is inclusive or exclusive scan is available only when Scan directive is encountered in lowering. 

Calling the callbacks or the function should happen when the scan directive (which appears in the body) is lowered by the frontend. Changing the function calls to callbacks does not add any other advantage, as weird combinations may be overlooked by frontend in both cases. 

At this point, I am not able to come up with a better design here. Please let me know your inputs. Happy to discuss more .

The example mentioned is an interesting one. The design should support, however will ensure this with my frontend changes

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


More information about the llvm-commits mailing list