Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does ProGraML capture Loop optimization (loop unroll) information during graph generation? #205

Open
cheukwaylee opened this issue Jan 27, 2023 · 1 comment
Labels
Question Discussion or request for further information

Comments

@cheukwaylee
Copy link

cheukwaylee commented Jan 27, 2023

❓ Questions

I know that ProGraML builds the graph by inserting a node for each instruction and variable/constant in the LLVM intermediate representation (IR), and then the final result is made of control flow, data flow, and call flow. But in this way, the Metadata information seems not to be included.

When I add a #pragma unroll in my source C code, there is NOT differece in corresponding LLVM IR except for addtional metadata !llvm.loop !2 following the instruction br.

And thus, the generated graph is the same except for one node representing the br instruction above. The only difference appears in that node's attribute features/full_text. However, the full_text attribute, which includes the entire line of raw LLVM IR information, is usually discarded when using the GNN model. That is, the graph generated with ProGraML does NOT contain any information about Loop optimization. This is a huge disaster for the project I am working on:(

In short, my questions are:

  1. Where the loop optimization information be included in LLVM IR, is it only in Metadata? I guess Yes after referring to LLVM-loop-unrolling.
  2. Does ProGraML use the Metadata information in LLVM IR to construct control flow, data flow, and call flow (as structural information)?

Thanks in advance for any help!!

My test case and corresponding produced results

source C code, then clang-7 -S -emit-llvm dot_product.c is used to obtain the LLVM IR.

#define n 2

void dot_product(int vec_a[n], int vec_b[n], int *res){
    *res = 0;
#pragma unroll 2 // with loop optimization
    for (int i = 0; i < n; i++){
        *res += vec_a[i] * vec_b[i];
    }
}

the corresponding partly LLVM IR and Graph with #pragma unroll 2

...
  br label %9, !llvm.loop !2
...
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.0.1-12 (tags/RELEASE_701/final)"}
!2 = distinct !{!2, !3}
!3 = !{!"llvm.loop.unroll.count", i32 2}
      <node id="67" label="67">
        <attvalues>
          <attvalue for="0" value="3" />
          <attvalue for="4" value="{'full_text': ['br label %9, !llvm.loop !2']}" />
          <attvalue for="1" value="0" />
          <attvalue for="2" value="br" />
          <attvalue for="3" value="0" />
        </attvalues>
      </node>

the corresponding partly LLVM IR and Graph without #pragma unroll 2

...
  br label %9
...
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.0.1-12 (tags/RELEASE_701/final)"}
      <node id="67" label="67">
        <attvalues>
          <attvalue for="0" value="3" />
          <attvalue for="4" value="{'full_text': ['br label %9']}" />
          <attvalue for="1" value="0" />
          <attvalue for="2" value="br" />
          <attvalue for="3" value="0" />
        </attvalues>
      </node>
@cheukwaylee cheukwaylee added the Question Discussion or request for further information label Jan 27, 2023
@ChrisCummins
Copy link
Owner

ChrisCummins commented Feb 11, 2023

  • Where the loop optimization information be included in LLVM IR, is it only in Metadata? I guess Yes after referring to LLVM-loop-unrolling.

That looks right to me.

  • Does ProGraML use the Metadata information in LLVM IR to construct control flow, data flow, and call flow (as structural information)?

It doesn't automatically add metadata info to instructions. You would need to extend ProgramGraphBuilder::AddLlvmInstruction() to pull out any extra metadata you want and embed it in the graph as node features. Here is an example of how we do this for instruction profiling data:

https://github.com/ChrisCummins/ProGraML/blob/development/programl/ir/llvm/internal/program_graph_builder.cc#L307-L319

I'd happily accept a patch that also records loop unroll information, as I think that would be useful.

#if PROGRAML_LLVM_VERSION_MAJOR > 3
// Add profiling information features, if available.
uint64_t profTotalWeight;
if (instruction->extractProfTotalWeight(profTotalWeight)) {
graph::AddScalarFeature(node, "llvm_profile_total_weight", profTotalWeight);
}
uint64_t profTrueWeight;
uint64_t profFalseWeight;
if (instruction->extractProfMetadata(profTrueWeight, profFalseWeight)) {
graph::AddScalarFeature(node, "llvm_profile_true_weight", profTrueWeight);
graph::AddScalarFeature(node, "llvm_profile_false_weight", profFalseWeight);
}
#endif

Cheers,
Chris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Discussion or request for further information
Projects
None yet
Development

No branches or pull requests

2 participants