1

I have auto-generated benchmark files in LLVM IR, but to extract loop bounds using another tool I need them in C. Converting to C is fairly simple using llvm-cbe/Julia (as suggested in comments in this post) or llvm2c. However, as far as I got, both tools represent the entire control flow with gotos, which makes the result a C program, but not much more usable. For example, this excerpt just represents some if/else constructs.

  gene_glob_input_variable = llvm_cbe_gene_arg;
  llvm_cbe_tmp__1 = gene_glob_input_variable;
  llvm_cbe_arith1 = llvm_cbe_tmp__1 ^ 253077109;
  llvm_cbe_tmp__2 = gene_glob_input_variable;
  if (((((llvm_cbe_tmp__2 & -129) == 0u)&1))) {
    goto llvm_cbe_br1_2e_then;
  } else {
    goto llvm_cbe_br1_2e_else_2e_wcp;
  }

llvm_cbe_br1_2e_then:
  llvm_cbe_tmp__3 = gene_glob_input_variable;
  *((uint32_t*) alloca(sizeof(uint32_t))) = llvm_cbe_tmp__3;
  llvm_cbe_tmp__4 = gene_glob_input_variable;
  *((uint32_t*) alloca(sizeof(uint32_t))) = llvm_cbe_tmp__4;
  goto llvm_cbe_br1_2e_end_2e_wcp;

llvm_cbe_br1_2e_else_2e_wcp:
  *(volatile uint16_t*)(&cons0) = (((uint16_t)llvm_cbe_tmp__1));
  *(volatile uint16_t*)(&cons0) = (((uint16_t)llvm_cbe_tmp__2));
  llvm_cbe_tmp__5 = gene_glob_input_variable;
  if (((((llvm_cbe_tmp__5 & -1048577) != 0u)&1))) {
    goto llvm_cbe_br2_2e_then_2e_wcp;
  } else {
    goto llvm_cbe_br2_2e_else;
  }

Is there a way to reconstruct if/else and for loops from C with gotos, or is there a way to use these tools to make that happen directly from .ll files? The constructs selected for generation in the benchmarks are fairly simple, so a solution with incomplete loop reconstruction would suffice. Thanks for any hint.

jf_
  • 3,099
  • 2
  • 13
  • 31
  • Are you willing to accept an answer describing how to do it by hand? If not; there's a chance this question is off-topic. Further use of the selected tools is on-topic but searching for another one is off-topic. – Joshua Apr 13 '21 at 15:31
  • Well, I was mostly hoping there was something that could be done with Julia/LLVM that I didn't see yet (llvm2c seems an even smaller project with less options). Otherwise, if there is a _manual_ way that I could automate in any way, at least for the obvious cases, then that would totally be a solution. Really rewriting everything by hand is not an option. – jf_ Apr 13 '21 at 15:40
  • @Joshua I assumed this is on-topic because [this post](https://stackoverflow.com/questions/5180914/llvm-ir-back-to-human-readable-source-language) that answered the first half of my question apparently was okay. Sorry if that assumption was wrong. – jf_ Apr 13 '21 at 15:45

1 Answers1

0

A bit late answer, but you might be interested in Rellic.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 01:07
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32725978) – user16217248 Sep 21 '22 at 18:40