24

I have a few lines of C++ code (Compiler Explorer) that compiles to a bit over 100 lines of filtered assembly code (no more irrelevant lines can be yanked from the output) on each of two compilers I tried. I don't understand why the compilers didn't optimize away a big part of the code that I thought should be easy to optimize away, tried a few tweaks that didn't work, and want to know the best way to induce the compilers into making that optimization.

  1. Is this a suitable question for StackOverflow?
  2. If so, how should I format the question?
    • Should I include (over 100 lines of) compiler-generated assembly code in my question?
      • Should the output of both compilers be included?
    • Should I include just the parts of the assembly that I find problematic (still unwieldy)?
    • Should I just include the link to Compiler Explorer?
  3. Should I also include all the failed code tweaks in my question?

Possibly related question on meta.SE: Please add the ability to fold blocks of code in questions and answers

9
  • 3
    "Possibly related question on meta.SE: Please add the ability to fold blocks of code in questions and answers" - Just for what it's worth, this can be done on Codidact, which allows details and summary HTML tags. Commented May 25 at 6:50
  • 3
    Whatever you do ask exactly 1 (clear specific researched non-duplicate) question. Presumably the complete context of the source code is relevant & given & an appropriate minimal reproducible example.
    – philipxy
    Commented May 25 at 8:27
  • 1
    "why the compilers didn't optimize away a big part of the code" This is compiler-specific & option-specific. So "context" includes relevant research results re compiler documentation (& compiler optimization generally) & your question should likely be re just 1 compiler & version.
    – philipxy
    Commented May 26 at 0:33
  • I'm also worried that nobody else will ever find that question, even if they have the same issue, which is also unlikely. Commented May 26 at 20:41
  • 1
    Is it possible for you to create a more minimal example/reproducer?
    – dan1st
    Commented May 27 at 6:34
  • 1
    If you are worried that 100 lines of assembly is too much: don't. It is not too much. If you have cut down the assembly to only that which is required to ask the question, then you're fine.
    – Ian Boyd
    Commented May 27 at 19:59
  • @dan1st I don't think I can. The code calls two methods on a std::vector, one of which is inside a loop; other than that the code does almost no computation.
    – fghzxm
    Commented May 28 at 8:20
  • "Is it acceptable to post" - Acceptable is really ambiguous. Are you referring to if it is allowed by the rules... or if it will be received well? Two different levels of quality control this site has and both should be considered individually. Yes to the former, I don't give it much hope for the latter unless you can somehow really make the question sparkle and be useful for other people as well. It would need to have less to do with the exact code you're presenting and more to do with the process of compiler optimization involved so the knowledge is repeatable for other similar code.
    – Gimby
    Commented May 28 at 11:23
  • It is worth throwing such code at MSVC latest and Intel ICX in addition to Clang. They each have weak spots and your example seems to have found one in Clang. TBH I prefer a link to Godbolt and the shorter sourcecode (or a representative sample of the bad code) but 100 lines isn't unreasonable for an MRE if all of them are required for a minimum reproducible example - and that seems to be the case here. Make sure you ask a clear unambiguous question. Commented May 30 at 8:01

1 Answer 1

14

Is this a suitable question for Stack Overflow?

It sounds like one to me - or at least, a suitable question could exist in this space. Ideally, your "few lines of C++ code" would demonstrate a general pattern of code that you find leads to this optimization failure.

I have a few lines of C++ code (Compiler Explorer) that compiles to a bit over 100 lines of filtered assembly code (no more irrelevant lines can be yanked from the output) on each of two compilers I tried.

In this circumstance, the C++ code is input and the generated assembly is output.

The generated assembly presumably assembles to machine code that can be executed without error, and which solves the problem that the original C++ code was written to perform. So there's no real sense in which it can be considered an "error message", so we aren't looking for completeness as we would in that case.

We should therefore treat the C++ according to normal standards for an MRE, and the assembly according to normal standards for program output.

Should I include just the parts of the compiler output that I find problematic (still unwieldy)?

Would an expert need to see these parts in order to understand the problem? I would guess not, in which case we should probably just see a description of what is wrong with the compiler output.

Ideally, we'd see the complete, correct expected output (I'm assuming this is considerably shorter), or at least a reasonable facsimile, along with a question about why the compiler doesn't give that result. Ideally, it would also be clear from the question, exactly why it should be reasonable to expect that the optimization could be done automatically (and that it doesn't require human intuition).

It could well turn out that the compiler is bound by some edge case that you haven't considered, such that your proposed optimization isn't actually valid unless the compiler can be given appropriate assurances (e.g., that two input memory ranges can't overlap). Assuming that you'd consider an answer that points this out to be correct, you're IMO likely on the right track with a question like this.

Should I just include the link to Compiler Explorer?

We cannot accept a question that "just" does so, regardless of your justification. If that's really the only reasonable way to avoid including hundreds of lines of "code" in your question, then that strongly counts against your question being suitable (as it suggests a question that Needs More Focus).

7
  • I've changed "compiler output" in the title to "compiler-generated assembly". Sorry if you were confused by it, and thanks for pointing it out.
    – fghzxm
    Commented May 25 at 7:06
  • 2
    I wasn't confused, it's fine. The point is, even if it's "code" for a future process, it's (output) data in the context of the question. Commented May 25 at 7:34
  • 2
    How would you feel about "abusing" the codesnippet feature for the 100 lines of assembly, given the code snippet has an option "hide snippet by default" which would reduce visual post length and only those that are interested can examine the details after a click. A downside is that it will not be runnable, despite having that button.
    – rene
    Commented May 25 at 7:46
  • 1
    ... I don't think I'd object to that, but it's clearly not how the site is intended to be used. Commented May 25 at 7:47
  • 6
    the usual code blocks are length-limited already, with scrollbars. I don't necessarily see the issue with that aspect. I see no need to hide or entirely collapse them. just leave them at the end. Commented May 26 at 20:42
  • 2
    Code Snippets are for HTML, CSS, and JS only, so any use of them for code in other languages should be avoided (and would be edited out if found by curators). Using them for other languages causes confusion because of syntax highlighting being incorrect due to those system-enforced language constraints, as well as confusion from expecting to be able to "run" the code which also obviously won't work. cc @rene It's no more acceptable than using code formatting or quote formatting for emphasis.
    – TylerH
    Commented May 27 at 13:27
  • The input/output distinction is what matters here. I agree that it's acceptable to post 100 lines of output assembly code if the input is brief. A similar situation sometimes arises in e.g. serialization, where a fairly simple data model can result in a fairly complex XSD Schema or JSON output. (Also, assembly code being what it is, any assembly-code [mcve] is likely to be more verbose than an equivalent one in a higher level language.)
    – dbc
    Commented May 27 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .