|
13 | 13 | #include <utility> |
14 | 14 | #include <vector> |
15 | 15 |
|
| 16 | +#include "xenia/base/assert.h" |
| 17 | + |
16 | 18 | namespace xe { |
17 | 19 | namespace gpu { |
18 | 20 |
|
@@ -101,5 +103,105 @@ spv::Id SpirvBuilder::createTriBuiltinCall(spv::Id result_type, |
101 | 103 | return result; |
102 | 104 | } |
103 | 105 |
|
| 106 | +SpirvBuilder::IfBuilder::IfBuilder(spv::Id condition, unsigned int control, |
| 107 | + SpirvBuilder& builder, |
| 108 | + unsigned int thenWeight, |
| 109 | + unsigned int elseWeight) |
| 110 | + : builder(builder), |
| 111 | + condition(condition), |
| 112 | + control(control), |
| 113 | + thenWeight(thenWeight), |
| 114 | + elseWeight(elseWeight), |
| 115 | + function(builder.getBuildPoint()->getParent()) { |
| 116 | + // Make the blocks, but only put the then-block into the function, the |
| 117 | + // else-block and merge-block will be added later, in order, after earlier |
| 118 | + // code is emitted. |
| 119 | + thenBlock = new spv::Block(builder.getUniqueId(), function); |
| 120 | + elseBlock = nullptr; |
| 121 | + mergeBlock = new spv::Block(builder.getUniqueId(), function); |
| 122 | + |
| 123 | + // Save the current block, so that we can add in the flow control split when |
| 124 | + // makeEndIf is called. |
| 125 | + headerBlock = builder.getBuildPoint(); |
| 126 | + |
| 127 | + spv::Id headerBlockId = headerBlock->getId(); |
| 128 | + thenPhiParent = headerBlockId; |
| 129 | + elsePhiParent = headerBlockId; |
| 130 | + |
| 131 | + function.addBlock(thenBlock); |
| 132 | + builder.setBuildPoint(thenBlock); |
| 133 | +} |
| 134 | + |
| 135 | +void SpirvBuilder::IfBuilder::makeBeginElse(bool branchToMerge) { |
| 136 | +#ifndef NDEBUG |
| 137 | + assert_true(currentBranch == Branch::kThen); |
| 138 | +#endif |
| 139 | + |
| 140 | + if (branchToMerge) { |
| 141 | + // Close out the "then" by having it jump to the mergeBlock. |
| 142 | + thenPhiParent = builder.getBuildPoint()->getId(); |
| 143 | + builder.createBranch(mergeBlock); |
| 144 | + } |
| 145 | + |
| 146 | + // Make the first else block and add it to the function. |
| 147 | + elseBlock = new spv::Block(builder.getUniqueId(), function); |
| 148 | + function.addBlock(elseBlock); |
| 149 | + |
| 150 | + // Start building the else block. |
| 151 | + builder.setBuildPoint(elseBlock); |
| 152 | + |
| 153 | +#ifndef NDEBUG |
| 154 | + currentBranch = Branch::kElse; |
| 155 | +#endif |
| 156 | +} |
| 157 | + |
| 158 | +void SpirvBuilder::IfBuilder::makeEndIf(bool branchToMerge) { |
| 159 | +#ifndef NDEBUG |
| 160 | + assert_true(currentBranch == Branch::kThen || currentBranch == Branch::kElse); |
| 161 | +#endif |
| 162 | + |
| 163 | + if (branchToMerge) { |
| 164 | + // Jump to the merge block. |
| 165 | + (elseBlock ? elsePhiParent : thenPhiParent) = |
| 166 | + builder.getBuildPoint()->getId(); |
| 167 | + builder.createBranch(mergeBlock); |
| 168 | + } |
| 169 | + |
| 170 | + // Go back to the headerBlock and make the flow control split. |
| 171 | + builder.setBuildPoint(headerBlock); |
| 172 | + builder.createSelectionMerge(mergeBlock, control); |
| 173 | + { |
| 174 | + spv::Block* falseBlock = elseBlock ? elseBlock : mergeBlock; |
| 175 | + std::unique_ptr<spv::Instruction> branch = |
| 176 | + std::make_unique<spv::Instruction>(spv::OpBranchConditional); |
| 177 | + branch->addIdOperand(condition); |
| 178 | + branch->addIdOperand(thenBlock->getId()); |
| 179 | + branch->addIdOperand(falseBlock->getId()); |
| 180 | + if (thenWeight || elseWeight) { |
| 181 | + branch->addImmediateOperand(thenWeight); |
| 182 | + branch->addImmediateOperand(elseWeight); |
| 183 | + } |
| 184 | + builder.getBuildPoint()->addInstruction(std::move(branch)); |
| 185 | + thenBlock->addPredecessor(builder.getBuildPoint()); |
| 186 | + falseBlock->addPredecessor(builder.getBuildPoint()); |
| 187 | + } |
| 188 | + |
| 189 | + // Add the merge block to the function. |
| 190 | + function.addBlock(mergeBlock); |
| 191 | + builder.setBuildPoint(mergeBlock); |
| 192 | + |
| 193 | +#ifndef NDEBUG |
| 194 | + currentBranch = Branch::kMerge; |
| 195 | +#endif |
| 196 | +} |
| 197 | + |
| 198 | +spv::Id SpirvBuilder::IfBuilder::createMergePhi(spv::Id then_variable, |
| 199 | + spv::Id else_variable) const { |
| 200 | + assert_true(builder.getBuildPoint() == mergeBlock); |
| 201 | + return builder.createQuadOp(spv::OpPhi, builder.getTypeId(then_variable), |
| 202 | + then_variable, getThenPhiParent(), else_variable, |
| 203 | + getElsePhiParent()); |
| 204 | +} |
| 205 | + |
104 | 206 | } // namespace gpu |
105 | 207 | } // namespace xe |
0 commit comments