[snippy] Add check of duplicated instructions in histogram#473
[snippy] Add check of duplicated instructions in histogram#473Maksim-Sebelev wants to merge 1 commit into
Conversation
| // FIXME: here we run probability visitor to collect information on | ||
| // which opcodes were already mentioned | ||
| Result.reinitProbabilityVisitor(); |
There was a problem hiding this comment.
This call runs expensive visitor so let's not call it inside a loop.
Here Result.contains() would not return true right after Result.insertTopOpcode(Opc, WeightRes). I am not sure how to do this better
|
I've come up with a smarter way to find duplicates, can you please review it? |
|
I've added the '-Wno-error=duplicated-instructions' option. Could you please review it again? I haven't documented this yet because I wasn't sure how and where to do it, could you please explain? About the 4 new similar tests: I can't combine them because the error/warning messages differ by the number of duplicates (there are 2 cases: exactly 1 duplicate, and more than 1 duplicate). :) |
f37c1f1 to
ecc0bf9
Compare
| sections: | ||
| - name: 1 | ||
| VMA: 0x210000 | ||
| SIZE: 0x100000 | ||
| LMA: 0x210000 | ||
| ACCESS: rx | ||
| - name: 2 | ||
| VMA: 0x400000 | ||
| SIZE: 0x100000 | ||
| LMA: 0x400000 | ||
| ACCESS: rw |
There was a problem hiding this comment.
Let's not duplicate this section and just reuse Inputs/section.yaml file
| - [LW, 1.0] | ||
| - [SW, 1.0] | ||
|
|
||
| # CHECK: error: (duplicated-instructions) multiple weight definition of DUPLICATED_INSTRUCTION: weight of this instruction will be initialized to the first weight in the histogram. |
There was a problem hiding this comment.
This looks weird.
Why do we say "weight of this instruction will be initialized to the first weight in the histogram." if it is an error?
There was a problem hiding this comment.
By the way, currently, when an opcode appears repeatedly, their weights are summed - this happens after adding histogram-patterns support. For example
histogram-patterns:
- AddSub: "ADD | SUB"
histogram:
- [pattern: AddSub, 1.0]
- [ADD, 1.0]
- [ADD, 1.0]
- [SUB, 1.0]|In the example above
AddWeight = 1.0 + 1.0 + 1 / 2 (from pattern) = 5 / 2
SubWeight = 1.0 + 1 / 2 (from pattern) = 3 / 2
TotalWeight = AddWeight + SubWeight = 8 / 2 = 4
And here's the question: is this patch even needed? After all, opcodes can now legitimately repeat (given patterns), and the total weight of an opcode is calculated from its weight in the top-level histogram and from patterns.
@arromanoff what do you think?
| if (DuplicatedInstructionsCont.empty()) // no duplicates | ||
| return; | ||
| std::ostringstream DuplicatedInstructionsMsg; | ||
| auto DuplicatedInstructionsIt = DuplicatedInstructionsCont.begin(); | ||
| DuplicatedInstructionsMsg << "multiple weight definition of " | ||
| << *DuplicatedInstructionsIt++; | ||
| std::for_each(DuplicatedInstructionsIt, DuplicatedInstructionsCont.end(), | ||
| [&DuplicatedInstructionsMsg](std::string_view Instruction) { | ||
| DuplicatedInstructionsMsg << ", " << Instruction; | ||
| }); |
There was a problem hiding this comment.
We already have llvm::interleaveComma function from STLExtras.h header.
And we don't use standard streams, llvm has its' own raw_string_ostream
| - [C_LUI_HINT, 4.26] | ||
| - [C_MV, 4.26] | ||
| - [C_MV_HINT, 4.26] | ||
| - [C_NOP, 4.26] |
There was a problem hiding this comment.
Redundant change
No, it was a duplicate, so this test has failed after chages from this PR (or I can use here ‘-Wno-error=duplicated-instructions‘ in options)
| - [C_ADDI, 4.66] | ||
| - [C_ADDIW, 4.66] | ||
| - [C_ADDI_HINT_IMM_ZERO, 4.66] | ||
| - [C_NOP, 4.66] |
| # RUN: sed -e s/DUPLICATED_INSTRUCTION/ADD/ %s > %t.1.yaml | ||
| # RUN: not llvm-snippy %t.1.yaml --model-plugin=None -mtriple=riscv64 \ | ||
| # RUN: |& FileCheck %t.1.yaml | ||
|
|
||
| # RUN: sed -e s/DUPLICATED_INSTRUCTION/ADDI/ %s > %t.2.yaml | ||
| # RUN: not llvm-snippy %t.2.yaml --model-plugin=None -mtriple=riscv64 \ | ||
| # RUN: |& FileCheck %t.2.yaml |
There was a problem hiding this comment.
I think these tests are excessive. Can we stop at 3-5 opcodes?
| Result.reinitProbabilityVisitor(); | ||
| return Result; | ||
| } | ||
| std::unordered_set<std::string_view> UniqueInstructions, |
There was a problem hiding this comment.
Please try using LLVM types where possible: StringSet instead of unordered_set (I think will be good here), and StringRef instead of string_view
| return Result; | ||
| } | ||
| std::unordered_set<std::string_view> UniqueInstructions, | ||
| DuplicatedInstructions; |
There was a problem hiding this comment.
It's better to declare one variable per line for readability
| - [LW, 1.0] | ||
| - [SW, 1.0] | ||
|
|
||
| # CHECK: error: (duplicated-instructions) multiple weight definition of DUPLICATED_INSTRUCTION: weight of this instruction will be initialized to the first weight in the histogram. |
There was a problem hiding this comment.
By the way, currently, when an opcode appears repeatedly, their weights are summed - this happens after adding histogram-patterns support. For example
histogram-patterns:
- AddSub: "ADD | SUB"
histogram:
- [pattern: AddSub, 1.0]
- [ADD, 1.0]
- [ADD, 1.0]
- [SUB, 1.0]|In the example above
AddWeight = 1.0 + 1.0 + 1 / 2 (from pattern) = 5 / 2
SubWeight = 1.0 + 1 / 2 (from pattern) = 3 / 2
TotalWeight = AddWeight + SubWeight = 8 / 2 = 4
And here's the question: is this patch even needed? After all, opcodes can now legitimately repeat (given patterns), and the total weight of an opcode is calculated from its weight in the top-level histogram and from patterns.
@arromanoff what do you think?
|
Done |
| Result.reinitProbabilityVisitor(); | ||
| return Result; | ||
| } | ||
| StringSet<> UniqueInstructions; |
There was a problem hiding this comment.
OpcodeHistogram class has TopOpcodes field to keep all top opcodes. Please, use this field and don't create redundant UniqueInstructions
There was a problem hiding this comment.
I agree. insertTopNode inserts into TopOpcodes member and we can ask it for contains. This way we don't need to add these UniqueInstructions and DuplicatedInstructions. Yes, we will report one instruction at a time but so be it. After internal discussion we decided that there is no need to report all at once
| Result.insertTopOpcode(Tgt.getInternalOpcode(Opc), WeightRes); | ||
| } | ||
| } | ||
| DuplicatedInstructions.begin(); |
| Result.reinitProbabilityVisitor(); | ||
| return Result; | ||
| } | ||
| StringSet<> UniqueInstructions; |
There was a problem hiding this comment.
I agree. insertTopNode inserts into TopOpcodes member and we can ask it for contains. This way we don't need to add these UniqueInstructions and DuplicatedInstructions. Yes, we will report one instruction at a time but so be it. After internal discussion we decided that there is no need to report all at once
|
Done |
a82c037 to
3658163
Compare
| break; | ||
| } | ||
|
|
||
| auto &&NameRef = NameInfo.Val; |
| continue; | ||
| } | ||
| } | ||
| auto Name = NameInfo.Val; |
There was a problem hiding this comment.
No need to create NameRef, you already have Name. Leave just one of them
| // FIXME: need to do something when OpcOpt is empty std::optional ??? | ||
| // now it just ignored |
There was a problem hiding this comment.
NameRef can be the name of the pattern - in this case you pass to OpcodeCache not an Opcode name and it gives you nullopt
|
Fixed |
|
In general LGTM |
45c7d95 to
428f80f
Compare
428f80f to
32ff28f
Compare
|
My bad, I've broken my branch. Now it's fixed, the status of the branch is similar to the last commit before this message |
97ed387 to
b9aee52
Compare
Hi!
This PR address the following issue - #470
Changes
In each iteration of the
we check if Result already contains Opc:
Resultis a SmallVector, soO(n)complexity (methodcontains) per iteration isn't a problem. Anyway, this can be just first simple solution.Verification
A test with a histogram containing a duplicated instruction was also added. This test and all other tests completed successfully on my device.
:)