|
| 1 | +// SPDX-FileCopyrightText : © 2025 TU Wien <vadl@tuwien.ac.at> |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package vadl.gcb.passes; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.IdentityHashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import vadl.configuration.GcbConfiguration; |
| 27 | +import vadl.pass.Pass; |
| 28 | +import vadl.pass.PassName; |
| 29 | +import vadl.pass.PassResults; |
| 30 | +import vadl.viam.Instruction; |
| 31 | +import vadl.viam.Specification; |
| 32 | +import vadl.viam.graph.Graph; |
| 33 | +import vadl.viam.graph.WritesRegisterTensor; |
| 34 | +import vadl.viam.graph.dependency.ProcCallNode; |
| 35 | +import vadl.viam.graph.dependency.ReadMemNode; |
| 36 | +import vadl.viam.graph.dependency.WriteMemNode; |
| 37 | +import vadl.viam.passes.SnapshotInstructionBehaviorPass; |
| 38 | + |
| 39 | +/** |
| 40 | + * Compute the intrinsic attributes for a {@link Instruction}. |
| 41 | + */ |
| 42 | +public class DetermineIntrinsicAttributesPass extends Pass { |
| 43 | + public DetermineIntrinsicAttributesPass(GcbConfiguration gcbConfiguration) { |
| 44 | + super(gcbConfiguration); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public PassName getName() { |
| 49 | + return new PassName("DetermineIntrinsicAttributesPass"); |
| 50 | + } |
| 51 | + |
| 52 | + @Nullable |
| 53 | + @Override |
| 54 | + public Object execute(PassResults passResults, Specification viam) throws IOException { |
| 55 | + var snapshots = |
| 56 | + (Map<Instruction, Graph>) passResults.lastResultOf(SnapshotInstructionBehaviorPass.class); |
| 57 | + IdentityHashMap<Instruction, List<InstructionIntrinsicAttributesCtx.Attribute>> map = |
| 58 | + new IdentityHashMap<>(); |
| 59 | + |
| 60 | + for (var instruction : viam.isa().orElseThrow().ownInstructions()) { |
| 61 | + var snapshot = Objects.requireNonNull(snapshots.get(instruction)); |
| 62 | + var isNoMem = isNoMem(snapshot); |
| 63 | + var willReturn = willReturn(snapshot); |
| 64 | + var speculatable = speculatable(snapshot); |
| 65 | + |
| 66 | + var attributes = new ArrayList<InstructionIntrinsicAttributesCtx.Attribute>(); |
| 67 | + if (isNoMem) { |
| 68 | + attributes.add(InstructionIntrinsicAttributesCtx.Attribute.NoMem); |
| 69 | + } |
| 70 | + if (willReturn) { |
| 71 | + attributes.add(InstructionIntrinsicAttributesCtx.Attribute.WillReturn); |
| 72 | + } else { |
| 73 | + attributes.add(InstructionIntrinsicAttributesCtx.Attribute.NoReturn); |
| 74 | + } |
| 75 | + if (speculatable) { |
| 76 | + attributes.add(InstructionIntrinsicAttributesCtx.Attribute.Speculatable); |
| 77 | + } |
| 78 | + |
| 79 | + map.put(instruction, attributes); |
| 80 | + instruction.attachExtension(new InstructionIntrinsicAttributesCtx(attributes)); |
| 81 | + } |
| 82 | + |
| 83 | + return map; |
| 84 | + } |
| 85 | + |
| 86 | + private boolean isMem(Graph snapshot) { |
| 87 | + return !snapshot.getNodes(WriteMemNode.class).toList().isEmpty() |
| 88 | + || !snapshot.getNodes(ReadMemNode.class).toList().isEmpty(); |
| 89 | + } |
| 90 | + |
| 91 | + private boolean isNoMem(Graph snapshot) { |
| 92 | + return !isMem(snapshot); |
| 93 | + } |
| 94 | + |
| 95 | + private boolean willReturn(Graph snapshot) { |
| 96 | + return !snapshot.getNodes(WritesRegisterTensor.class).toList().isEmpty(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Compute a special attribute for LLVM. |
| 101 | + * The following conditions must hold: |
| 102 | + * x) No side effects |
| 103 | + * x) Does not write memory |
| 104 | + * x) Does not perform I/O |
| 105 | + * x) Does not change global state |
| 106 | + * x) Cannot trap or fault |
| 107 | + * x) No division-by-zero traps |
| 108 | + */ |
| 109 | + private boolean speculatable(Graph snapshot) { |
| 110 | + return isNoMem(snapshot) && snapshot.getNodes(ProcCallNode.class).toList().isEmpty(); |
| 111 | + } |
| 112 | +} |
0 commit comments