Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 15 | #include "Debug.hpp" |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 16 | #include "EmulatedIntrinsics.hpp" |
| 17 | #include "OptimalIntrinsics.hpp" |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 18 | #include "Print.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 19 | #include "Reactor.hpp" |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 20 | #include "ReactorDebugInfo.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 22 | #include "ExecutableMemory.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 23 | #include "Optimizer.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 25 | #include "src/IceCfg.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 26 | #include "src/IceCfgNode.h" |
| 27 | #include "src/IceELFObjectWriter.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 28 | #include "src/IceELFStreamer.h" |
| 29 | #include "src/IceGlobalContext.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 30 | #include "src/IceGlobalInits.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 31 | #include "src/IceTypes.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 32 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 34 | #include "llvm/Support/FileSystem.h" |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 36 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 37 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 38 | #include "marl/event.h" |
| 39 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 40 | #if __has_feature(memory_sanitizer) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 41 | # include <sanitizer/msan_interface.h> |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 42 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 43 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 44 | #if defined(_WIN32) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 45 | # ifndef WIN32_LEAN_AND_MEAN |
| 46 | # define WIN32_LEAN_AND_MEAN |
| 47 | # endif // !WIN32_LEAN_AND_MEAN |
| 48 | # ifndef NOMINMAX |
| 49 | # define NOMINMAX |
| 50 | # endif // !NOMINMAX |
| 51 | # include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 52 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 53 | |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 54 | #include <array> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 55 | #include <iostream> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 56 | #include <limits> |
| 57 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 58 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 59 | // Subzero utility functions |
| 60 | // These functions only accept and return Subzero (Ice) types, and do not access any globals. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 61 | namespace { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 62 | namespace sz { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 63 | |
| 64 | Ice::Cfg *createFunction(Ice::GlobalContext *context, Ice::Type returnType, const std::vector<Ice::Type> ¶mTypes) |
| 65 | { |
| 66 | uint32_t sequenceNumber = 0; |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame] | 67 | auto *function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 68 | |
| 69 | function->setStackSizeLimit(512 * 1024); // 512 KiB |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 70 | |
| 71 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 72 | |
| 73 | for(auto type : paramTypes) |
| 74 | { |
| 75 | Ice::Variable *arg = function->makeVariable(type); |
| 76 | function->addArg(arg); |
| 77 | } |
| 78 | |
| 79 | Ice::CfgNode *node = function->makeNode(); |
| 80 | function->setEntryNode(node); |
| 81 | |
| 82 | return function; |
| 83 | } |
| 84 | |
| 85 | Ice::Type getPointerType(Ice::Type elementType) |
| 86 | { |
| 87 | if(sizeof(void *) == 8) |
| 88 | { |
| 89 | return Ice::IceType_i64; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | return Ice::IceType_i32; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 98 | { |
| 99 | int typeSize = Ice::typeWidthInBytes(type); |
| 100 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 101 | |
| 102 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 103 | auto address = function->makeVariable(getPointerType(type)); |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 104 | auto alloca = Ice::InstAlloca::create(function, address, bytes, typeSize); // SRoA depends on the alignment to match the type size. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 105 | function->getEntryNode()->getInsts().push_front(alloca); |
| 106 | |
| 107 | return address; |
| 108 | } |
| 109 | |
| 110 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 111 | { |
| 112 | if(sizeof(void *) == 8) |
| 113 | { |
| 114 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 119 | } |
| 120 | } |
| 121 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 122 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 123 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 124 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 125 | // Wrapper for calls on C functions with Ice types |
| 126 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 127 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 128 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 129 | |
| 130 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 131 | // then truncate result to bool. |
| 132 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 133 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 134 | if(returningBool) |
| 135 | { |
| 136 | ret = function->makeVariable(Ice::IceType_i32); |
| 137 | } |
| 138 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 139 | { |
| 140 | ret = function->makeVariable(retTy); |
| 141 | } |
| 142 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 143 | auto call = Ice::InstCall::create(function, iceArgs.size(), ret, callTarget, false, false, isVariadic); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 144 | for(auto arg : iceArgs) |
| 145 | { |
| 146 | call->addArg(arg); |
| 147 | } |
| 148 | |
| 149 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 150 | |
| 151 | if(returningBool) |
| 152 | { |
| 153 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 154 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 155 | } |
| 156 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 157 | return ret; |
| 158 | } |
| 159 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 160 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 161 | { |
| 162 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 163 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 164 | } |
| 165 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 166 | // Wrapper for calls on C functions with Ice types |
| 167 | template<typename Return, typename... CArgs, typename... RArgs> |
Nicolas Capens | 629bf95 | 2022-01-18 15:08:14 -0500 | [diff] [blame] | 168 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Return(fptr)(CArgs...), RArgs &&...args) |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 169 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 170 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 171 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 172 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 173 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 174 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 177 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 178 | { |
| 179 | Ice::Variable *to = function->makeVariable(toType); |
| 180 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 181 | basicBlock->appendInst(cast); |
| 182 | return to; |
| 183 | } |
| 184 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 185 | Ice::Variable *createLoad(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *ptr, Ice::Type type, unsigned int align) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 186 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 187 | Ice::Variable *result = function->makeVariable(type); |
| 188 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 189 | basicBlock->appendInst(load); |
| 190 | |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 195 | } // namespace |
| 196 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 197 | namespace rr { |
| 198 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 199 | class CoroutineGenerator; |
| 200 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 201 | |
| 202 | namespace { |
| 203 | |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 204 | // Used to automatically invoke llvm_shutdown() when driver is unloaded |
| 205 | llvm::llvm_shutdown_obj llvmShutdownObj; |
| 206 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 207 | // Default configuration settings. Must be accessed under mutex lock. |
| 208 | std::mutex defaultConfigLock; |
| 209 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 210 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 211 | // This uses a static in a function to avoid the cost of a global static |
| 212 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 213 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 214 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 215 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 218 | Ice::GlobalContext *context = nullptr; |
| 219 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 220 | Ice::CfgNode *entryBlock = nullptr; |
| 221 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 222 | Ice::CfgNode *basicBlock = nullptr; |
| 223 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 224 | rr::ELFMemoryStreamer *routine = nullptr; |
| 225 | |
| 226 | std::mutex codegenMutex; |
| 227 | |
| 228 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 229 | Ice::Fdstream *out = nullptr; |
| 230 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 231 | // Coroutine globals |
| 232 | rr::Type *coroYieldType = nullptr; |
| 233 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 234 | marl::Scheduler &getOrCreateScheduler() |
| 235 | { |
| 236 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 237 | marl::Scheduler::Config cfg; |
| 238 | cfg.setWorkerThreadCount(8); |
| 239 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 240 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 241 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 242 | return *scheduler; |
| 243 | } |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 244 | |
| 245 | rr::Nucleus::OptimizerCallback *optimizerCallback = nullptr; |
| 246 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 247 | } // Anonymous namespace |
| 248 | |
| 249 | namespace { |
| 250 | |
| 251 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 252 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 253 | #endif |
| 254 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 255 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 256 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 257 | #endif |
| 258 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 259 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 260 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 261 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 262 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 263 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
| 264 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 265 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 266 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
| 267 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 268 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 269 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 270 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 273 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 274 | { |
| 275 | switch(memoryOrder) |
| 276 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 277 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 278 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 279 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 280 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 281 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 282 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 283 | } |
| 284 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 285 | } |
| 286 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 287 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 288 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 289 | public: |
| 290 | const static bool ARM; |
| 291 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 292 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 293 | private: |
| 294 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 295 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 296 | #if defined(__i386__) || defined(__x86_64__) |
| 297 | # if defined(_WIN32) |
| 298 | __cpuid(registers, info); |
| 299 | # else |
| 300 | __asm volatile("cpuid" |
| 301 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 302 | : "a"(info)); |
| 303 | # endif |
| 304 | #else |
| 305 | registers[0] = 0; |
| 306 | registers[1] = 0; |
| 307 | registers[2] = 0; |
| 308 | registers[3] = 0; |
| 309 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Sean Risser | 46a649d | 2021-08-30 15:44:33 -0400 | [diff] [blame] | 312 | constexpr static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 313 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 314 | #if defined(__arm__) || defined(__aarch64__) |
| 315 | return true; |
| 316 | #elif defined(__i386__) || defined(__x86_64__) |
| 317 | return false; |
| 318 | #elif defined(__mips__) |
| 319 | return false; |
| 320 | #else |
| 321 | # error "Unknown architecture" |
| 322 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 323 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 324 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 325 | static bool detectSSE4_1() |
| 326 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 327 | #if defined(__i386__) || defined(__x86_64__) |
| 328 | int registers[4]; |
| 329 | cpuid(registers, 1); |
| 330 | return (registers[2] & 0x00080000) != 0; |
| 331 | #else |
| 332 | return false; |
| 333 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 334 | } |
| 335 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 336 | |
Sean Risser | 46a649d | 2021-08-30 15:44:33 -0400 | [diff] [blame] | 337 | constexpr bool CPUID::ARM = CPUID::detectARM(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 338 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
Sean Risser | 46a649d | 2021-08-30 15:44:33 -0400 | [diff] [blame] | 339 | constexpr bool emulateIntrinsics = false; |
| 340 | constexpr bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 341 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 342 | constexpr bool subzeroDumpEnabled = false; |
| 343 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 344 | |
| 345 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 346 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 347 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 348 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 349 | |
| 350 | } // anonymous namespace |
| 351 | |
| 352 | namespace rr { |
| 353 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 354 | std::string BackendName() |
| 355 | { |
| 356 | return "Subzero"; |
| 357 | } |
| 358 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 359 | const Capabilities Caps = { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 360 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | enum EmulatedType |
| 364 | { |
| 365 | EmulatedShift = 16, |
| 366 | EmulatedV2 = 2 << EmulatedShift, |
| 367 | EmulatedV4 = 4 << EmulatedShift, |
| 368 | EmulatedV8 = 8 << EmulatedShift, |
| 369 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 370 | |
| 371 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 372 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 373 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 374 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 375 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 376 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 377 | }; |
| 378 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 379 | class Value : public Ice::Operand |
| 380 | {}; |
| 381 | class SwitchCases : public Ice::InstSwitch |
| 382 | {}; |
| 383 | class BasicBlock : public Ice::CfgNode |
| 384 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 385 | |
| 386 | Ice::Type T(Type *t) |
| 387 | { |
| 388 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 389 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 392 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 393 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 394 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | Type *T(EmulatedType t) |
| 398 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 399 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 400 | } |
| 401 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 402 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 403 | { |
| 404 | std::vector<Ice::Type> result; |
| 405 | result.reserve(types.size()); |
| 406 | for(auto &t : types) |
| 407 | { |
| 408 | result.push_back(T(t)); |
| 409 | } |
| 410 | return result; |
| 411 | } |
| 412 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 413 | Value *V(Ice::Operand *v) |
| 414 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 415 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 418 | Ice::Operand *V(Value *v) |
| 419 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 420 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 423 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 424 | { |
| 425 | std::vector<Ice::Operand *> result; |
| 426 | result.reserve(values.size()); |
| 427 | for(auto &v : values) |
| 428 | { |
| 429 | result.push_back(V(v)); |
| 430 | } |
| 431 | return result; |
| 432 | } |
| 433 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 434 | BasicBlock *B(Ice::CfgNode *b) |
| 435 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 436 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static size_t typeSize(Type *type) |
| 440 | { |
| 441 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 442 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 443 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 444 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 445 | case Type_v2i32: return 8; |
| 446 | case Type_v4i16: return 8; |
| 447 | case Type_v2i16: return 4; |
| 448 | case Type_v8i8: return 8; |
| 449 | case Type_v4i8: return 4; |
| 450 | case Type_v2f32: return 8; |
| 451 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | return Ice::typeWidthInBytes(T(type)); |
| 456 | } |
| 457 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 458 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 459 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 460 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 461 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 462 | { |
| 463 | Nucleus::createRetVoid(); |
| 464 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 465 | |
| 466 | // Connect the entry block to the top of the initial basic block |
| 467 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 468 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 471 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 472 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 473 | |
| 474 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 475 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 476 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 480 | { |
| 481 | return §ionHeader(elfHeader)[index]; |
| 482 | } |
| 483 | |
| 484 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 485 | { |
| 486 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 487 | |
| 488 | uint32_t index = relocation.getSymbol(); |
| 489 | int table = relocationTable.sh_link; |
| 490 | void *symbolValue = nullptr; |
| 491 | |
| 492 | if(index != SHN_UNDEF) |
| 493 | { |
| 494 | if(table == SHN_UNDEF) return nullptr; |
| 495 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 496 | |
| 497 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 498 | if(index >= symtab_entries) |
| 499 | { |
| 500 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 501 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 504 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 505 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 506 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 507 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 508 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 509 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 510 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 511 | symbolValue = reinterpret_cast<void *>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 512 | } |
| 513 | else |
| 514 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 515 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 516 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 519 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 520 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 521 | |
| 522 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 523 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 524 | switch(relocation.getType()) |
| 525 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 526 | case R_ARM_NONE: |
| 527 | // No relocation |
| 528 | break; |
| 529 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 530 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 531 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 532 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 533 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 534 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 535 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 536 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 537 | { |
| 538 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 539 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 540 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 541 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 542 | default: |
| 543 | ASSERT(false && "Unsupported relocation type"); |
| 544 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 545 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 546 | } |
| 547 | else |
| 548 | { |
| 549 | switch(relocation.getType()) |
| 550 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 551 | case R_386_NONE: |
| 552 | // No relocation |
| 553 | break; |
| 554 | case R_386_32: |
| 555 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 556 | break; |
| 557 | case R_386_PC32: |
| 558 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 559 | break; |
| 560 | default: |
| 561 | ASSERT(false && "Unsupported relocation type"); |
| 562 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 563 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 564 | } |
| 565 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 566 | return symbolValue; |
| 567 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 568 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 569 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 570 | { |
| 571 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 572 | |
| 573 | uint32_t index = relocation.getSymbol(); |
| 574 | int table = relocationTable.sh_link; |
| 575 | void *symbolValue = nullptr; |
| 576 | |
| 577 | if(index != SHN_UNDEF) |
| 578 | { |
| 579 | if(table == SHN_UNDEF) return nullptr; |
| 580 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 581 | |
| 582 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 583 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 584 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 585 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 586 | return nullptr; |
| 587 | } |
| 588 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 589 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 590 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 591 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 592 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 593 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 594 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 595 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 596 | symbolValue = reinterpret_cast<void *>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 597 | } |
| 598 | else |
| 599 | { |
| 600 | return nullptr; |
| 601 | } |
| 602 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 603 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 604 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 605 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 606 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 607 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 608 | switch(relocation.getType()) |
| 609 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 610 | case R_X86_64_NONE: |
| 611 | // No relocation |
| 612 | break; |
| 613 | case R_X86_64_64: |
| 614 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 615 | break; |
| 616 | case R_X86_64_PC32: |
| 617 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 618 | break; |
| 619 | case R_X86_64_32S: |
| 620 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 621 | break; |
| 622 | default: |
| 623 | ASSERT(false && "Unsupported relocation type"); |
| 624 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return symbolValue; |
| 628 | } |
| 629 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 630 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 631 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 632 | const void *entry; |
| 633 | size_t codeSize = 0; |
| 634 | }; |
| 635 | |
| 636 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 637 | { |
| 638 | ASSERT(functionNames.size() > 0); |
| 639 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 640 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 641 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 642 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 643 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 644 | if(!elfHeader->checkMagic()) |
| 645 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 646 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 650 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 651 | #if defined(__i386__) |
| 652 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 653 | #elif defined(__x86_64__) |
| 654 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 655 | #elif defined(__arm__) |
| 656 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 657 | #elif defined(__aarch64__) |
| 658 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 659 | #elif defined(__mips__) |
| 660 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 661 | #else |
| 662 | # error "Unsupported platform" |
| 663 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 664 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 665 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 666 | |
| 667 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 668 | { |
| 669 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 670 | { |
| 671 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 672 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 673 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 674 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 675 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 676 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 677 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 678 | { |
| 679 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 680 | { |
| 681 | return j; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 686 | return static_cast<size_t>(-1); |
| 687 | }; |
| 688 | |
| 689 | size_t index = findSectionNameEntryIndex(); |
| 690 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 691 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 692 | } |
| 693 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 694 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 695 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 696 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 697 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 698 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 699 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 700 | const Elf32_Rel &relocation = ((const Elf32_Rel *)(elfImage + sectionHeader[i].sh_offset))[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 701 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 702 | } |
| 703 | } |
| 704 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 705 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 706 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 707 | |
| 708 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 709 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 710 | const Elf64_Rela &relocation = ((const Elf64_Rela *)(elfImage + sectionHeader[i].sh_offset))[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 711 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 716 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | template<typename T> |
| 720 | struct ExecutableAllocator |
| 721 | { |
| 722 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 723 | template<class U> |
| 724 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 725 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 726 | |
| 727 | using value_type = T; |
| 728 | using size_type = std::size_t; |
| 729 | |
| 730 | T *allocate(size_type n) |
| 731 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 732 | return (T *)allocateMemoryPages( |
| 733 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void deallocate(T *p, size_type n) |
| 737 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 738 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 739 | } |
| 740 | }; |
| 741 | |
| 742 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 743 | { |
| 744 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 745 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 746 | |
| 747 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 748 | ELFMemoryStreamer() |
| 749 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 750 | { |
| 751 | position = 0; |
| 752 | buffer.reserve(0x1000); |
| 753 | } |
| 754 | |
| 755 | ~ELFMemoryStreamer() override |
| 756 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void write8(uint8_t Value) override |
| 760 | { |
| 761 | if(position == (uint64_t)buffer.size()) |
| 762 | { |
| 763 | buffer.push_back(Value); |
| 764 | position++; |
| 765 | } |
| 766 | else if(position < (uint64_t)buffer.size()) |
| 767 | { |
| 768 | buffer[position] = Value; |
| 769 | position++; |
| 770 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 771 | else |
| 772 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | void writeBytes(llvm::StringRef Bytes) override |
| 776 | { |
| 777 | std::size_t oldSize = buffer.size(); |
| 778 | buffer.resize(oldSize + Bytes.size()); |
| 779 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 780 | position += Bytes.size(); |
| 781 | } |
| 782 | |
| 783 | uint64_t tell() const override { return position; } |
| 784 | |
| 785 | void seek(uint64_t Off) override { position = Off; } |
| 786 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 787 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 788 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 789 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 790 | |
| 791 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 792 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 793 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 794 | for(auto &entryPoint : entryPoints) |
| 795 | { |
| 796 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 797 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 798 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 799 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 800 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 801 | } |
| 802 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 803 | void finalize() |
| 804 | { |
| 805 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 806 | |
| 807 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 808 | } |
| 809 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 810 | void setEntry(int index, const void *func) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 811 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 812 | ASSERT(func); |
| 813 | funcs[index] = func; |
| 814 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 815 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 816 | const void *getEntry(int index) const override |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 817 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 818 | ASSERT(funcs[index]); |
| 819 | return funcs[index]; |
| 820 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 821 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 822 | const void *addConstantData(const void *data, size_t size, size_t alignment = 1) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 823 | { |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 824 | // Check if we already have a suitable constant. |
| 825 | for(const auto &c : constantsPool) |
| 826 | { |
| 827 | void *ptr = c.data.get(); |
| 828 | size_t space = c.space; |
| 829 | |
| 830 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 831 | |
| 832 | if(space < size) |
| 833 | { |
| 834 | continue; |
| 835 | } |
| 836 | |
| 837 | if(memcmp(data, alignedPtr, size) == 0) |
| 838 | { |
| 839 | return alignedPtr; |
| 840 | } |
| 841 | } |
| 842 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 843 | // TODO(b/148086935): Replace with a buffer allocator. |
| 844 | size_t space = size + alignment; |
| 845 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 846 | void *ptr = buf.get(); |
| 847 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 848 | ASSERT(alignedPtr); |
| 849 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 850 | constantsPool.emplace_back(std::move(buf), space); |
| 851 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 852 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 853 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 854 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 855 | private: |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 856 | struct Constant |
| 857 | { |
| 858 | Constant(std::unique_ptr<uint8_t[]> data, size_t space) |
| 859 | : data(std::move(data)) |
| 860 | , space(space) |
| 861 | {} |
| 862 | |
| 863 | std::unique_ptr<uint8_t[]> data; |
| 864 | size_t space; |
| 865 | }; |
| 866 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 867 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 868 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 869 | std::size_t position; |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 870 | std::vector<Constant> constantsPool; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 871 | }; |
| 872 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 873 | #ifdef ENABLE_RR_PRINT |
| 874 | void VPrintf(const std::vector<Value *> &vals) |
| 875 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 876 | sz::Call(::function, ::basicBlock, Ice::IceType_i32, reinterpret_cast<const void *>(rr::DebugPrintf), V(vals), true); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 877 | } |
| 878 | #endif // ENABLE_RR_PRINT |
| 879 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 880 | Nucleus::Nucleus() |
| 881 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 882 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 883 | |
| 884 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 885 | Ice::ClFlags::getParsedClFlags(Flags); |
| 886 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 887 | #if defined(__arm__) |
| 888 | Flags.setTargetArch(Ice::Target_ARM32); |
| 889 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 890 | #elif defined(__mips__) |
| 891 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 892 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 893 | #else // x86 |
| 894 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 895 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 896 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 897 | Flags.setOutFileType(Ice::FT_Elf); |
| 898 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 899 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 900 | Flags.setDisableHybridAssembly(true); |
| 901 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 902 | // Emit functions into separate sections in the ELF so we can find them by name |
| 903 | Flags.setFunctionSections(true); |
| 904 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 905 | static llvm::raw_os_ostream cout(std::cout); |
| 906 | static llvm::raw_os_ostream cerr(std::cerr); |
| 907 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 908 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 909 | { |
| 910 | // Decorate text asm with liveness info |
| 911 | Flags.setDecorateAsm(true); |
| 912 | } |
| 913 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 914 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 915 | { |
| 916 | std::error_code errorCode; |
| 917 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 918 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 919 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 920 | } |
| 921 | else |
| 922 | { |
| 923 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 924 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 925 | ::routine = elfMemory; |
| 926 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 927 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 928 | #if !__has_feature(memory_sanitizer) |
| 929 | // thread_local variables in shared libraries are initialized at load-time, |
| 930 | // but this is not observed by MemorySanitizer if the loader itself was not |
Nicolas Capens | af90770 | 2021-05-14 11:10:49 -0400 | [diff] [blame] | 931 | // instrumented, leading to false-positive uninitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 932 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 933 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 934 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | Nucleus::~Nucleus() |
| 938 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 939 | delete Variable::unmaterializedVariables; |
| 940 | Variable::unmaterializedVariables = nullptr; |
| 941 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 942 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 943 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 944 | |
| 945 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 946 | ::allocator = nullptr; |
| 947 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 948 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 949 | ::function = nullptr; |
| 950 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 951 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 952 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 953 | |
| 954 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 955 | ::elfFile = nullptr; |
| 956 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 957 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 958 | ::out = nullptr; |
| 959 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 960 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 961 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 962 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 963 | |
| 964 | ::codegenMutex.unlock(); |
| 965 | } |
| 966 | |
| 967 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 968 | { |
| 969 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 970 | ::defaultConfig() = cfg; |
| 971 | } |
| 972 | |
| 973 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 974 | { |
| 975 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 976 | auto &config = ::defaultConfig(); |
| 977 | config = cfgEdit.apply(config); |
| 978 | } |
| 979 | |
| 980 | Config Nucleus::getDefaultConfig() |
| 981 | { |
| 982 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 983 | return ::defaultConfig(); |
| 984 | } |
| 985 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 986 | // This function lowers and produces executable binary code in memory for the input functions, |
| 987 | // and returns a Routine with the entry points to these functions. |
| 988 | template<size_t Count> |
Sean Risser | 705231f | 2021-08-19 18:17:24 -0400 | [diff] [blame] | 989 | static std::shared_ptr<Routine> acquireRoutine(Ice::Cfg *const (&functions)[Count], const char *const (&names)[Count], const Config::Edit *cfgEdit) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 990 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 991 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 992 | // and GlobalContext::emitItems. |
| 993 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 994 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 995 | { |
| 996 | // Output dump strings immediately, rather than once buffer is full. Useful for debugging. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 997 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | ::context->emitFileHeader(); |
| 1001 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1002 | // Translate |
| 1003 | |
| 1004 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1005 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1006 | Ice::Cfg *currFunc = functions[i]; |
| 1007 | |
| 1008 | // Install function allocator in TLS for Cfg-specific container allocators |
| 1009 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 1010 | |
| 1011 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 1012 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 1013 | if(::optimizerCallback) |
| 1014 | { |
| 1015 | Nucleus::OptimizerReport report; |
| 1016 | rr::optimize(currFunc, &report); |
| 1017 | ::optimizerCallback(&report); |
| 1018 | ::optimizerCallback = nullptr; |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | rr::optimize(currFunc); |
| 1023 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1024 | |
| 1025 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1026 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1027 | |
| 1028 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1029 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1030 | |
| 1031 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1032 | |
| 1033 | if(subzeroEmitTextAsm) |
| 1034 | { |
| 1035 | currFunc->emit(); |
| 1036 | } |
| 1037 | |
| 1038 | currFunc->emitIAS(); |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame] | 1039 | |
| 1040 | if(currFunc->hasError()) |
| 1041 | { |
| 1042 | return nullptr; |
| 1043 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1044 | } |
| 1045 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1046 | // Emit items |
| 1047 | |
| 1048 | ::context->lowerGlobals(""); |
| 1049 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1050 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1051 | |
| 1052 | for(size_t i = 0; i < Count; ++i) |
| 1053 | { |
| 1054 | Ice::Cfg *currFunc = functions[i]; |
| 1055 | |
| 1056 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1057 | auto globals = currFunc->getGlobalInits(); |
| 1058 | if(globals && !globals->empty()) |
| 1059 | { |
| 1060 | ::context->getGlobals()->merge(globals.get()); |
| 1061 | } |
| 1062 | |
| 1063 | auto assembler = currFunc->releaseAssembler(); |
| 1064 | assembler->alignFunction(); |
| 1065 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1066 | } |
| 1067 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1068 | ::context->lowerGlobals("last"); |
| 1069 | ::context->lowerConstants(); |
| 1070 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1071 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1072 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1073 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1074 | objectWriter->writeNonUserSections(); |
| 1075 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1076 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1077 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1078 | ASSERT(entryPoints.size() == Count); |
| 1079 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1080 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1081 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1085 | |
| 1086 | Routine *handoffRoutine = ::routine; |
| 1087 | ::routine = nullptr; |
| 1088 | |
| 1089 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1090 | } |
| 1091 | |
Sean Risser | 705231f | 2021-08-19 18:17:24 -0400 | [diff] [blame] | 1092 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit *cfgEdit /* = nullptr */) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1093 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1094 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1095 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1096 | } |
| 1097 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1098 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1099 | { |
| 1100 | Ice::Type type = T(t); |
| 1101 | int typeSize = Ice::typeWidthInBytes(type); |
| 1102 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1103 | |
| 1104 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1105 | auto address = ::function->makeVariable(T(getPointerType(t))); |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 1106 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); // SRoA depends on the alignment to match the type size. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1107 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1108 | |
| 1109 | return V(address); |
| 1110 | } |
| 1111 | |
| 1112 | BasicBlock *Nucleus::createBasicBlock() |
| 1113 | { |
| 1114 | return B(::function->makeNode()); |
| 1115 | } |
| 1116 | |
| 1117 | BasicBlock *Nucleus::getInsertBlock() |
| 1118 | { |
| 1119 | return B(::basicBlock); |
| 1120 | } |
| 1121 | |
| 1122 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1123 | { |
Nicolas Capens | 7c296ec | 2021-02-18 14:10:26 -0500 | [diff] [blame] | 1124 | // ASSERT(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1125 | |
| 1126 | ::basicBlock = basicBlock; |
| 1127 | } |
| 1128 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1129 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1130 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1131 | ASSERT(::function == nullptr); |
| 1132 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1133 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1134 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1135 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1136 | |
| 1137 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1138 | |
| 1139 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1140 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1141 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1142 | // TODO: Get rid of this as a global, and create scoped allocs in every Nucleus function instead. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1143 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1144 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1145 | ::entryBlock = ::function->getEntryNode(); |
| 1146 | ::basicBlock = ::function->makeNode(); |
| 1147 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | Value *Nucleus::getArgument(unsigned int index) |
| 1151 | { |
| 1152 | return V(::function->getArgs()[index]); |
| 1153 | } |
| 1154 | |
| 1155 | void Nucleus::createRetVoid() |
| 1156 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1157 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1158 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1159 | // Code generated after this point is unreachable, so any variables |
| 1160 | // being read can safely return an undefined value. We have to avoid |
| 1161 | // materializing variables after the terminator ret instruction. |
| 1162 | Variable::killUnmaterialized(); |
| 1163 | |
| 1164 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1165 | ::basicBlock->appendInst(ret); |
| 1166 | } |
| 1167 | |
| 1168 | void Nucleus::createRet(Value *v) |
| 1169 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1170 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1171 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1172 | // Code generated after this point is unreachable, so any variables |
| 1173 | // being read can safely return an undefined value. We have to avoid |
| 1174 | // materializing variables after the terminator ret instruction. |
| 1175 | Variable::killUnmaterialized(); |
| 1176 | |
| 1177 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1178 | ::basicBlock->appendInst(ret); |
| 1179 | } |
| 1180 | |
| 1181 | void Nucleus::createBr(BasicBlock *dest) |
| 1182 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1183 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1184 | Variable::materializeAll(); |
| 1185 | |
| 1186 | auto br = Ice::InstBr::create(::function, dest); |
| 1187 | ::basicBlock->appendInst(br); |
| 1188 | } |
| 1189 | |
| 1190 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1191 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1192 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1193 | Variable::materializeAll(); |
| 1194 | |
| 1195 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1196 | ::basicBlock->appendInst(br); |
| 1197 | } |
| 1198 | |
| 1199 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1200 | { |
| 1201 | switch(op) |
| 1202 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1203 | case Ice::InstArithmetic::Add: |
| 1204 | case Ice::InstArithmetic::Fadd: |
| 1205 | case Ice::InstArithmetic::Mul: |
| 1206 | case Ice::InstArithmetic::Fmul: |
| 1207 | case Ice::InstArithmetic::And: |
| 1208 | case Ice::InstArithmetic::Or: |
| 1209 | case Ice::InstArithmetic::Xor: |
| 1210 | return true; |
| 1211 | default: |
| 1212 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1217 | { |
| 1218 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1219 | |
| 1220 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1221 | |
| 1222 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1223 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1224 | ::basicBlock->appendInst(arithmetic); |
| 1225 | |
| 1226 | return V(result); |
| 1227 | } |
| 1228 | |
| 1229 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1230 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1231 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1232 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
| 1233 | } |
| 1234 | |
| 1235 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1236 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1237 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1238 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
| 1239 | } |
| 1240 | |
| 1241 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1242 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1243 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1244 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
| 1245 | } |
| 1246 | |
| 1247 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1248 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1249 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1250 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
| 1251 | } |
| 1252 | |
| 1253 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1254 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1255 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1256 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
| 1257 | } |
| 1258 | |
| 1259 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1260 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1261 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1262 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
| 1263 | } |
| 1264 | |
| 1265 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1266 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1267 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1268 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
| 1269 | } |
| 1270 | |
| 1271 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1272 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1273 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1274 | return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs); |
| 1275 | } |
| 1276 | |
| 1277 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1278 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1279 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1280 | return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs); |
| 1281 | } |
| 1282 | |
| 1283 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1284 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1285 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1286 | return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs); |
| 1287 | } |
| 1288 | |
| 1289 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1290 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1291 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1292 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
| 1293 | } |
| 1294 | |
| 1295 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1296 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1297 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1298 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1299 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1300 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1301 | return nullptr; |
| 1302 | } |
| 1303 | |
| 1304 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1305 | { |
| 1306 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1310 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1311 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1312 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
| 1313 | } |
| 1314 | |
| 1315 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1316 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1317 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1318 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
| 1319 | } |
| 1320 | |
| 1321 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1322 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1323 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1324 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
| 1325 | } |
| 1326 | |
| 1327 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1328 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1329 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1330 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
| 1331 | } |
| 1332 | |
| 1333 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1334 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1335 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1336 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
| 1337 | } |
| 1338 | |
| 1339 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1340 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1341 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1342 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1343 | } |
| 1344 | |
| 1345 | Value *Nucleus::createNeg(Value *v) |
| 1346 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1347 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1348 | return createSub(createNullValue(T(v->getType())), v); |
| 1349 | } |
| 1350 | |
| 1351 | Value *Nucleus::createFNeg(Value *v) |
| 1352 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1353 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1354 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1355 | Value *negativeZero = Ice::isVectorType(v->getType()) ? createConstantVector(c, T(v->getType())) : V(::context->getConstantFloat(-0.0f)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1356 | |
| 1357 | return createFSub(negativeZero, v); |
| 1358 | } |
| 1359 | |
| 1360 | Value *Nucleus::createNot(Value *v) |
| 1361 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1362 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1363 | if(Ice::isScalarIntegerType(v->getType())) |
| 1364 | { |
| 1365 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1366 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1367 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1368 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1369 | int64_t c[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1370 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1371 | } |
| 1372 | } |
| 1373 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1374 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1375 | { |
| 1376 | #if defined(__i386__) || defined(__x86_64__) |
| 1377 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1378 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1379 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1380 | #else |
| 1381 | if(atomic) |
| 1382 | { |
| 1383 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1384 | } |
| 1385 | if(memoryOrder != std::memory_order_relaxed) |
| 1386 | { |
| 1387 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1388 | } |
| 1389 | #endif |
| 1390 | |
| 1391 | // Vulkan doesn't allow sequential memory order |
| 1392 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1393 | } |
| 1394 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1395 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1396 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1397 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1398 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1399 | |
| 1400 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1401 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1402 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1403 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1404 | { |
| 1405 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1406 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1407 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1408 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1409 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1410 | Int x = *Pointer<Int>(pointer); |
| 1411 | |
| 1412 | Int4 vector; |
| 1413 | vector = Insert(vector, x, 0); |
| 1414 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1415 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1417 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1418 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1419 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1420 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1421 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1422 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1423 | Int x = *Pointer<Int>(pointer); |
| 1424 | Int y = *Pointer<Int>(pointer + 4); |
| 1425 | |
| 1426 | Int4 vector; |
| 1427 | vector = Insert(vector, x, 0); |
| 1428 | vector = Insert(vector, y, 1); |
| 1429 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1430 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1431 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1432 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1433 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1434 | else |
| 1435 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1436 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1437 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1438 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1439 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1440 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 1441 | auto load = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1442 | load->addArg(ptr); |
| 1443 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1444 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1445 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1446 | } |
| 1447 | else |
| 1448 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1449 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1450 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1451 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1452 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1453 | return V(result); |
| 1454 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1455 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1456 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1457 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1458 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1459 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1460 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1461 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1462 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1463 | if(align != 0) |
| 1464 | { |
| 1465 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1466 | call->addArg(ptr); |
| 1467 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1468 | ::basicBlock->appendInst(call); |
| 1469 | } |
| 1470 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1471 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1472 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1473 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1474 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1475 | { |
| 1476 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1477 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1478 | if(typeSize(type) == 4) |
| 1479 | { |
| 1480 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1481 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1482 | ::basicBlock->appendInst(bitcast); |
| 1483 | |
| 1484 | RValue<Int4> v(V(vector)); |
| 1485 | |
| 1486 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1487 | Int x = Extract(v, 0); |
| 1488 | *Pointer<Int>(pointer) = x; |
| 1489 | } |
| 1490 | else if(typeSize(type) == 8) |
| 1491 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1492 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1493 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1494 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1495 | ::basicBlock->appendInst(bitcast); |
| 1496 | |
| 1497 | RValue<Int4> v(V(vector)); |
| 1498 | |
| 1499 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1500 | Int x = Extract(v, 0); |
| 1501 | *Pointer<Int>(pointer) = x; |
| 1502 | Int y = Extract(v, 1); |
| 1503 | *Pointer<Int>(pointer + 4) = y; |
| 1504 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1505 | else |
| 1506 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1507 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1508 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1509 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1510 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 1511 | auto store = Ice::InstIntrinsic::create(::function, 3, nullptr, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1512 | store->addArg(value); |
| 1513 | store->addArg(ptr); |
| 1514 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1515 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1516 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1517 | } |
| 1518 | else |
| 1519 | { |
| 1520 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1521 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1522 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1523 | ::basicBlock->appendInst(store); |
| 1524 | } |
| 1525 | |
| 1526 | return value; |
| 1527 | } |
| 1528 | |
| 1529 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1530 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1531 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1532 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1533 | |
| 1534 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1535 | { |
| 1536 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1537 | |
| 1538 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1539 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1540 | return ptr; |
| 1541 | } |
| 1542 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1543 | return createAdd(ptr, createConstantInt(offset)); |
| 1544 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1545 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1546 | if(!Ice::isByteSizedType(T(type))) |
| 1547 | { |
| 1548 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1549 | } |
| 1550 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1551 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1552 | { |
| 1553 | if(unsignedIndex) |
| 1554 | { |
| 1555 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1556 | } |
| 1557 | else |
| 1558 | { |
| 1559 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | return createAdd(ptr, index); |
| 1564 | } |
| 1565 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1566 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1567 | { |
| 1568 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1569 | |
| 1570 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicRMW, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 1571 | auto inst = Ice::InstIntrinsic::create(::function, 0, result, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1572 | auto op = ::context->getConstantInt32(rmwOp); |
| 1573 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1574 | inst->addArg(op); |
| 1575 | inst->addArg(ptr); |
| 1576 | inst->addArg(value); |
| 1577 | inst->addArg(order); |
| 1578 | ::basicBlock->appendInst(inst); |
| 1579 | |
| 1580 | return V(result); |
| 1581 | } |
| 1582 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1583 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1584 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1585 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1586 | return createAtomicRMW(Ice::Intrinsics::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1590 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1591 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1592 | return createAtomicRMW(Ice::Intrinsics::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1596 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1597 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1598 | return createAtomicRMW(Ice::Intrinsics::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1602 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1603 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1604 | return createAtomicRMW(Ice::Intrinsics::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1608 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1609 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1610 | return createAtomicRMW(Ice::Intrinsics::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1614 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1615 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1616 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1620 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1621 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1622 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1623 | |
| 1624 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicCmpxchg, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 1625 | auto inst = Ice::InstIntrinsic::create(::function, 0, result, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1626 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1627 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1628 | inst->addArg(ptr); |
| 1629 | inst->addArg(compare); |
| 1630 | inst->addArg(value); |
| 1631 | inst->addArg(orderEq); |
| 1632 | inst->addArg(orderNeq); |
| 1633 | ::basicBlock->appendInst(inst); |
| 1634 | |
| 1635 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1639 | { |
| 1640 | if(v->getType() == T(destType)) |
| 1641 | { |
| 1642 | return v; |
| 1643 | } |
| 1644 | |
| 1645 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1646 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1647 | ::basicBlock->appendInst(cast); |
| 1648 | |
| 1649 | return V(result); |
| 1650 | } |
| 1651 | |
| 1652 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1653 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1654 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1655 | return createCast(Ice::InstCast::Trunc, v, destType); |
| 1656 | } |
| 1657 | |
| 1658 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1659 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1660 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1661 | return createCast(Ice::InstCast::Zext, v, destType); |
| 1662 | } |
| 1663 | |
| 1664 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1665 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1666 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1667 | return createCast(Ice::InstCast::Sext, v, destType); |
| 1668 | } |
| 1669 | |
| 1670 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1671 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1672 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1673 | return createCast(Ice::InstCast::Fptoui, v, destType); |
| 1674 | } |
| 1675 | |
| 1676 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1677 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1678 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1679 | return createCast(Ice::InstCast::Fptosi, v, destType); |
| 1680 | } |
| 1681 | |
| 1682 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1683 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1684 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1685 | return createCast(Ice::InstCast::Sitofp, v, destType); |
| 1686 | } |
| 1687 | |
| 1688 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1689 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1690 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1691 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
| 1692 | } |
| 1693 | |
| 1694 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1695 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1696 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1697 | return createCast(Ice::InstCast::Fpext, v, destType); |
| 1698 | } |
| 1699 | |
| 1700 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1701 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1702 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1703 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1704 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1705 | // emulate them by writing to the stack and reading back as the destination type. |
| 1706 | if(emulateMismatchedBitCast) |
| 1707 | { |
| 1708 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1709 | { |
| 1710 | Value *address = allocateStackVariable(destType); |
| 1711 | createStore(v, address, T(v->getType())); |
| 1712 | return createLoad(address, destType); |
| 1713 | } |
| 1714 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1715 | { |
| 1716 | Value *address = allocateStackVariable(T(v->getType())); |
| 1717 | createStore(v, address, T(v->getType())); |
| 1718 | return createLoad(address, destType); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1723 | } |
| 1724 | |
| 1725 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1726 | { |
| 1727 | ASSERT(lhs->getType() == rhs->getType()); |
| 1728 | |
| 1729 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1730 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1731 | ::basicBlock->appendInst(cmp); |
| 1732 | |
| 1733 | return V(result); |
| 1734 | } |
| 1735 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1736 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1737 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1738 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1739 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1740 | } |
| 1741 | |
| 1742 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1743 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1744 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1745 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1746 | } |
| 1747 | |
| 1748 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1749 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1750 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1751 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1752 | } |
| 1753 | |
| 1754 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1755 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1756 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1757 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1758 | } |
| 1759 | |
| 1760 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1761 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1762 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1763 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1764 | } |
| 1765 | |
| 1766 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1767 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1768 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1769 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1770 | } |
| 1771 | |
| 1772 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1773 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1774 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1775 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1776 | } |
| 1777 | |
| 1778 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1779 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1780 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1781 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1782 | } |
| 1783 | |
| 1784 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1785 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1786 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1787 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1788 | } |
| 1789 | |
| 1790 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1791 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1792 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1793 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1794 | } |
| 1795 | |
| 1796 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1797 | { |
| 1798 | ASSERT(lhs->getType() == rhs->getType()); |
| 1799 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1800 | |
| 1801 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1802 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1803 | ::basicBlock->appendInst(cmp); |
| 1804 | |
| 1805 | return V(result); |
| 1806 | } |
| 1807 | |
| 1808 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1809 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1810 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1811 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
| 1812 | } |
| 1813 | |
| 1814 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1815 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1816 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1817 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
| 1818 | } |
| 1819 | |
| 1820 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1821 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1822 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1823 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
| 1824 | } |
| 1825 | |
| 1826 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1827 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1828 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1829 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
| 1830 | } |
| 1831 | |
| 1832 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1833 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1834 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1835 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
| 1836 | } |
| 1837 | |
| 1838 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1839 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1840 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1841 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
| 1842 | } |
| 1843 | |
| 1844 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1845 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1846 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1847 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
| 1848 | } |
| 1849 | |
| 1850 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1851 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1852 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1853 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
| 1854 | } |
| 1855 | |
| 1856 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1857 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1858 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1859 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
| 1860 | } |
| 1861 | |
| 1862 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1863 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1864 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1865 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
| 1866 | } |
| 1867 | |
| 1868 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1869 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1870 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1871 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
| 1872 | } |
| 1873 | |
| 1874 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1875 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1876 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1877 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
| 1878 | } |
| 1879 | |
| 1880 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1881 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1882 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1883 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
| 1884 | } |
| 1885 | |
| 1886 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1887 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1888 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1889 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1890 | } |
| 1891 | |
| 1892 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1893 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1894 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1895 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1896 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1897 | ::basicBlock->appendInst(extract); |
| 1898 | |
| 1899 | return V(result); |
| 1900 | } |
| 1901 | |
| 1902 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1903 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1904 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1905 | auto result = ::function->makeVariable(vector->getType()); |
| 1906 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1907 | ::basicBlock->appendInst(insert); |
| 1908 | |
| 1909 | return V(result); |
| 1910 | } |
| 1911 | |
| 1912 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 1913 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1914 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1915 | ASSERT(V1->getType() == V2->getType()); |
| 1916 | |
| 1917 | int size = Ice::typeNumElements(V1->getType()); |
| 1918 | auto result = ::function->makeVariable(V1->getType()); |
| 1919 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1920 | |
| 1921 | for(int i = 0; i < size; i++) |
| 1922 | { |
| 1923 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1924 | } |
| 1925 | |
| 1926 | ::basicBlock->appendInst(shuffle); |
| 1927 | |
| 1928 | return V(result); |
| 1929 | } |
| 1930 | |
| 1931 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1932 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1933 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1934 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1935 | |
| 1936 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1937 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1938 | ::basicBlock->appendInst(select); |
| 1939 | |
| 1940 | return V(result); |
| 1941 | } |
| 1942 | |
| 1943 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1944 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1945 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1946 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1947 | ::basicBlock->appendInst(switchInst); |
| 1948 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1949 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1953 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1954 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1955 | switchCases->addBranch(label, label, branch); |
| 1956 | } |
| 1957 | |
| 1958 | void Nucleus::createUnreachable() |
| 1959 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1960 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1961 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1962 | ::basicBlock->appendInst(unreachable); |
| 1963 | } |
| 1964 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1965 | Type *Nucleus::getType(Value *value) |
| 1966 | { |
| 1967 | return T(V(value)->getType()); |
| 1968 | } |
| 1969 | |
| 1970 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1971 | { |
| 1972 | Ice::Type vecTy = T(vectorType); |
| 1973 | switch(vecTy) |
| 1974 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1975 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1976 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1977 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1978 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1979 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1980 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1981 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1982 | default: |
| 1983 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 1984 | return {}; |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1988 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1989 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1990 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1991 | } |
| 1992 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1993 | static constexpr Ice::Type getNaturalIntType() |
| 1994 | { |
| 1995 | constexpr size_t intSize = sizeof(int); |
| 1996 | static_assert(intSize == 4 || intSize == 8, ""); |
| 1997 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 1998 | } |
| 1999 | |
| 2000 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 2001 | { |
| 2002 | Ice::Type valueTy = T(valueType); |
| 2003 | switch(valueTy) |
| 2004 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2005 | case Ice::IceType_i32: |
| 2006 | return T(getNaturalIntType()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2007 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2008 | case Ice::IceType_f32: |
| 2009 | return T(Ice::IceType_f64); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2010 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2011 | default: |
| 2012 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 2013 | return {}; |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2017 | Value *Nucleus::createNullValue(Type *Ty) |
| 2018 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2019 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2020 | if(Ice::isVectorType(T(Ty))) |
| 2021 | { |
| 2022 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2023 | int64_t c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2024 | return createConstantVector(c, Ty); |
| 2025 | } |
| 2026 | else |
| 2027 | { |
| 2028 | return V(::context->getConstantZero(T(Ty))); |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | Value *Nucleus::createConstantLong(int64_t i) |
| 2033 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2034 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2035 | return V(::context->getConstantInt64(i)); |
| 2036 | } |
| 2037 | |
| 2038 | Value *Nucleus::createConstantInt(int i) |
| 2039 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2040 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2041 | return V(::context->getConstantInt32(i)); |
| 2042 | } |
| 2043 | |
| 2044 | Value *Nucleus::createConstantInt(unsigned int i) |
| 2045 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2046 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2047 | return V(::context->getConstantInt32(i)); |
| 2048 | } |
| 2049 | |
| 2050 | Value *Nucleus::createConstantBool(bool b) |
| 2051 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2052 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2053 | return V(::context->getConstantInt1(b)); |
| 2054 | } |
| 2055 | |
| 2056 | Value *Nucleus::createConstantByte(signed char i) |
| 2057 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2058 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2059 | return V(::context->getConstantInt8(i)); |
| 2060 | } |
| 2061 | |
| 2062 | Value *Nucleus::createConstantByte(unsigned char i) |
| 2063 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2064 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2065 | return V(::context->getConstantInt8(i)); |
| 2066 | } |
| 2067 | |
| 2068 | Value *Nucleus::createConstantShort(short i) |
| 2069 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2071 | return V(::context->getConstantInt16(i)); |
| 2072 | } |
| 2073 | |
| 2074 | Value *Nucleus::createConstantShort(unsigned short i) |
| 2075 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2076 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2077 | return V(::context->getConstantInt16(i)); |
| 2078 | } |
| 2079 | |
| 2080 | Value *Nucleus::createConstantFloat(float x) |
| 2081 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2082 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2083 | return V(::context->getConstantFloat(x)); |
| 2084 | } |
| 2085 | |
| 2086 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2087 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2088 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2089 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2090 | } |
| 2091 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2092 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2093 | { |
| 2094 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2095 | } |
| 2096 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2097 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2098 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2099 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2100 | const int vectorSize = 16; |
| 2101 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2102 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2103 | |
| 2104 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2105 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2106 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2107 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2108 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2109 | |
| 2110 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2111 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2112 | case Ice::IceType_v4i32: |
| 2113 | case Ice::IceType_v4i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2114 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2115 | const int initializer[4] = { (int)i[0], (int)i[1], (int)i[2], (int)i[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2116 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2117 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2118 | } |
| 2119 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2120 | case Ice::IceType_v4f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2121 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2122 | const float initializer[4] = { (float)f[0], (float)f[1], (float)f[2], (float)f[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2123 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2124 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2125 | } |
| 2126 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2127 | case Ice::IceType_v8i16: |
| 2128 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2129 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2130 | const short initializer[8] = { (short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[4], (short)i[5], (short)i[6], (short)i[7] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2131 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2132 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2133 | } |
| 2134 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2135 | case Ice::IceType_v16i8: |
| 2136 | case Ice::IceType_v16i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2137 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2138 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[8], (char)i[9], (char)i[10], (char)i[11], (char)i[12], (char)i[13], (char)i[14], (char)i[15] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2139 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2140 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2141 | } |
| 2142 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2143 | case Type_v2i32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2144 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2145 | const int initializer[4] = { (int)i[0], (int)i[1], (int)i[0], (int)i[1] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2146 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2147 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2148 | } |
| 2149 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2150 | case Type_v2f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2151 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2152 | const float initializer[4] = { (float)f[0], (float)f[1], (float)f[0], (float)f[1] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2153 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2154 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2155 | } |
| 2156 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2157 | case Type_v4i16: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2158 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2159 | const short initializer[8] = { (short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[0], (short)i[1], (short)i[2], (short)i[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2160 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2161 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2162 | } |
| 2163 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2164 | case Type_v8i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2165 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2166 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2167 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2168 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2169 | } |
| 2170 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2171 | case Type_v4i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2172 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2173 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2174 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2175 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2176 | } |
| 2177 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2178 | default: |
| 2179 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2180 | } |
| 2181 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2182 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2183 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2184 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2185 | return V(result); |
| 2186 | } |
| 2187 | |
| 2188 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2189 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2190 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2191 | } |
| 2192 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2193 | Value *Nucleus::createConstantString(const char *v) |
| 2194 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2195 | // NOTE: Do not call RR_DEBUG_INFO_UPDATE_LOC() here to avoid recursion when called from rr::Printv |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2196 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2197 | } |
| 2198 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 2199 | void Nucleus::setOptimizerCallback(OptimizerCallback *callback) |
| 2200 | { |
| 2201 | ::optimizerCallback = callback; |
| 2202 | } |
| 2203 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2204 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2205 | { |
| 2206 | return T(Ice::IceType_void); |
| 2207 | } |
| 2208 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2209 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2210 | { |
| 2211 | return T(Ice::IceType_i1); |
| 2212 | } |
| 2213 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2214 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2215 | { |
| 2216 | return T(Ice::IceType_i8); |
| 2217 | } |
| 2218 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2219 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2220 | { |
| 2221 | return T(Ice::IceType_i8); |
| 2222 | } |
| 2223 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2224 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2225 | { |
| 2226 | return T(Ice::IceType_i16); |
| 2227 | } |
| 2228 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2229 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2230 | { |
| 2231 | return T(Ice::IceType_i16); |
| 2232 | } |
| 2233 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2234 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2235 | { |
| 2236 | return T(Type_v4i8); |
| 2237 | } |
| 2238 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2239 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2240 | { |
| 2241 | return T(Type_v4i8); |
| 2242 | } |
| 2243 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2244 | namespace { |
| 2245 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2246 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2247 | return Byte(IfThenElse(Int(x) > 0xFF, Int(0xFF), IfThenElse(Int(x) < 0, Int(0), Int(x)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2248 | } |
| 2249 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2250 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2251 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2252 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2256 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2257 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } // namespace |
| 2260 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2261 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2262 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2263 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2264 | if(emulateIntrinsics) |
| 2265 | { |
| 2266 | Byte8 result; |
| 2267 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2268 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2269 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2270 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2271 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2272 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2273 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2274 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2275 | |
| 2276 | return result; |
| 2277 | } |
| 2278 | else |
| 2279 | { |
| 2280 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2281 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2282 | auto paddusb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2283 | paddusb->addArg(x.value()); |
| 2284 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2285 | ::basicBlock->appendInst(paddusb); |
| 2286 | |
| 2287 | return RValue<Byte8>(V(result)); |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2292 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2293 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2294 | if(emulateIntrinsics) |
| 2295 | { |
| 2296 | Byte8 result; |
| 2297 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2298 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2299 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2300 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2301 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2302 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2303 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2304 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2305 | |
| 2306 | return result; |
| 2307 | } |
| 2308 | else |
| 2309 | { |
| 2310 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2311 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2312 | auto psubusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2313 | psubusw->addArg(x.value()); |
| 2314 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2315 | ::basicBlock->appendInst(psubusw); |
| 2316 | |
| 2317 | return RValue<Byte8>(V(result)); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 2322 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2323 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2324 | return RValue<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2328 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2329 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2330 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2334 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2335 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2336 | if(emulateIntrinsics) |
| 2337 | { |
| 2338 | SByte8 result; |
| 2339 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2340 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2341 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2342 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2343 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2344 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2345 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2346 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2347 | |
| 2348 | return result; |
| 2349 | } |
| 2350 | else |
| 2351 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2352 | #if defined(__i386__) || defined(__x86_64__) |
| 2353 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2354 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2355 | RValue<Short4> lo = As<Short4>(As<UShort4>((As<Short4>(lhs) << 8) >> rhs) >> 8); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2356 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2357 | return As<SByte8>(hi | lo); |
| 2358 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2359 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2360 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2361 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2362 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2363 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2364 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2367 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2368 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2369 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2370 | return Int(Extract(xx, 0)) | Int(Extract(xx, 1)) | Int(Extract(xx, 2)) | Int(Extract(xx, 3)) | Int(Extract(xx, 4)) | Int(Extract(xx, 5)) | Int(Extract(xx, 6)) | Int(Extract(xx, 7)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2371 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2372 | else |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2373 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2374 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2375 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2376 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2377 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2378 | ::basicBlock->appendInst(movmsk); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2379 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2380 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2381 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2382 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2383 | |
| 2384 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2385 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2386 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2387 | // } |
| 2388 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2389 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2390 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2391 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2392 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2393 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2394 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2395 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2396 | { |
| 2397 | return T(Type_v8i8); |
| 2398 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2399 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2400 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2401 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2402 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2403 | // } |
| 2404 | |
| 2405 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2406 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2407 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2408 | // } |
| 2409 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2410 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 2411 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2412 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2413 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2414 | } |
| 2415 | |
| 2416 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2417 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2418 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2419 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2420 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2421 | SByte8 result; |
| 2422 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2423 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2424 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2425 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2426 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2427 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2428 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2429 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2430 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2431 | return result; |
| 2432 | } |
| 2433 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2434 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2435 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2436 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2437 | auto paddsb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2438 | paddsb->addArg(x.value()); |
| 2439 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2440 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2441 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2442 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2443 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2444 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2445 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2446 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2447 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2448 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2449 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2450 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2451 | SByte8 result; |
| 2452 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2453 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2454 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2455 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2456 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2457 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2458 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2459 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2460 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2461 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2462 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2463 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2464 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2465 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2466 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2467 | auto psubsb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2468 | psubsb->addArg(x.value()); |
| 2469 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2470 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2471 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2472 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2473 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2474 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2475 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2476 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2477 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2478 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2479 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2480 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2481 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2482 | return Int(Extract(xx, 0)) | Int(Extract(xx, 1)) | Int(Extract(xx, 2)) | Int(Extract(xx, 3)) | Int(Extract(xx, 4)) | Int(Extract(xx, 5)) | Int(Extract(xx, 6)) | Int(Extract(xx, 7)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2483 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2484 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2485 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2486 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2487 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2488 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2489 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2490 | ::basicBlock->appendInst(movmsk); |
| 2491 | |
| 2492 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2493 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2494 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2495 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2496 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 2497 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2498 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2499 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2500 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2501 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2502 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2503 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2504 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2505 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2506 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2507 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2508 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2509 | { |
| 2510 | return T(Type_v8i8); |
| 2511 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2512 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2513 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2514 | { |
| 2515 | return T(Ice::IceType_v16i8); |
| 2516 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2517 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2518 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2519 | { |
| 2520 | return T(Ice::IceType_v16i8); |
| 2521 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2522 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2523 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2524 | { |
| 2525 | return T(Type_v2i16); |
| 2526 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2527 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2528 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2529 | { |
| 2530 | return T(Type_v2i16); |
| 2531 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2532 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2533 | Short4::Short4(RValue<Int4> cast) |
| 2534 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2535 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2536 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2537 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2538 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2539 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2540 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2541 | |
| 2542 | storeValue(short4); |
| 2543 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2544 | |
| 2545 | // Short4::Short4(RValue<Float> cast) |
| 2546 | // { |
| 2547 | // } |
| 2548 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2549 | Short4::Short4(RValue<Float4> cast) |
| 2550 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2551 | // TODO(b/150791192): Generalize and optimize |
| 2552 | auto smin = std::numeric_limits<short>::min(); |
| 2553 | auto smax = std::numeric_limits<short>::max(); |
| 2554 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2558 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2559 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2560 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2561 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2562 | Short4 result; |
| 2563 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2564 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2565 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2566 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2567 | |
| 2568 | return result; |
| 2569 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2570 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2571 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2572 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2573 | } |
| 2574 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2575 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2576 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2577 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2578 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2579 | if(emulateIntrinsics) |
| 2580 | { |
| 2581 | Short4 result; |
| 2582 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2583 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2584 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2585 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2586 | |
| 2587 | return result; |
| 2588 | } |
| 2589 | else |
| 2590 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2591 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2596 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2597 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2598 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2599 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2600 | ::basicBlock->appendInst(cmp); |
| 2601 | |
| 2602 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2603 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2604 | ::basicBlock->appendInst(select); |
| 2605 | |
| 2606 | return RValue<Short4>(V(result)); |
| 2607 | } |
| 2608 | |
| 2609 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2610 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2611 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2612 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2613 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2614 | ::basicBlock->appendInst(cmp); |
| 2615 | |
| 2616 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2617 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2618 | ::basicBlock->appendInst(select); |
| 2619 | |
| 2620 | return RValue<Short4>(V(result)); |
| 2621 | } |
| 2622 | |
| 2623 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 2624 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2625 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2626 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2627 | } |
| 2628 | |
| 2629 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2630 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2631 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2632 | if(emulateIntrinsics) |
| 2633 | { |
| 2634 | Short4 result; |
| 2635 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2636 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2637 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2638 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2639 | |
| 2640 | return result; |
| 2641 | } |
| 2642 | else |
| 2643 | { |
| 2644 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2645 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2646 | auto paddsw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2647 | paddsw->addArg(x.value()); |
| 2648 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2649 | ::basicBlock->appendInst(paddsw); |
| 2650 | |
| 2651 | return RValue<Short4>(V(result)); |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2656 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2657 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2658 | if(emulateIntrinsics) |
| 2659 | { |
| 2660 | Short4 result; |
| 2661 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2662 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2663 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2664 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2665 | |
| 2666 | return result; |
| 2667 | } |
| 2668 | else |
| 2669 | { |
| 2670 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2671 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2672 | auto psubsw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2673 | psubsw->addArg(x.value()); |
| 2674 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2675 | ::basicBlock->appendInst(psubsw); |
| 2676 | |
| 2677 | return RValue<Short4>(V(result)); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2682 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2683 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2684 | if(emulateIntrinsics) |
| 2685 | { |
| 2686 | Short4 result; |
| 2687 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2688 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2689 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2690 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2691 | |
| 2692 | return result; |
| 2693 | } |
| 2694 | else |
| 2695 | { |
| 2696 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2697 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2698 | auto pmulhw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2699 | pmulhw->addArg(x.value()); |
| 2700 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2701 | ::basicBlock->appendInst(pmulhw); |
| 2702 | |
| 2703 | return RValue<Short4>(V(result)); |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2708 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2709 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2710 | if(emulateIntrinsics) |
| 2711 | { |
| 2712 | Int2 result; |
| 2713 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2714 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2715 | |
| 2716 | return result; |
| 2717 | } |
| 2718 | else |
| 2719 | { |
| 2720 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2721 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2722 | auto pmaddwd = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2723 | pmaddwd->addArg(x.value()); |
| 2724 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2725 | ::basicBlock->appendInst(pmaddwd); |
| 2726 | |
| 2727 | return As<Int2>(V(result)); |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2732 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2733 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2734 | if(emulateIntrinsics) |
| 2735 | { |
| 2736 | SByte8 result; |
| 2737 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2738 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2739 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2740 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2741 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2742 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2743 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2744 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2745 | |
| 2746 | return result; |
| 2747 | } |
| 2748 | else |
| 2749 | { |
| 2750 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2751 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2752 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2753 | pack->addArg(x.value()); |
| 2754 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2755 | ::basicBlock->appendInst(pack); |
| 2756 | |
| 2757 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2762 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2763 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2764 | if(emulateIntrinsics) |
| 2765 | { |
| 2766 | Byte8 result; |
| 2767 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2768 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2769 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2770 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2771 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2772 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2773 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2774 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2775 | |
| 2776 | return result; |
| 2777 | } |
| 2778 | else |
| 2779 | { |
| 2780 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2781 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2782 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2783 | pack->addArg(x.value()); |
| 2784 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2785 | ::basicBlock->appendInst(pack); |
| 2786 | |
| 2787 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2792 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2793 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2794 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2798 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2799 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2800 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2801 | } |
| 2802 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2803 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2804 | { |
| 2805 | return T(Type_v4i16); |
| 2806 | } |
| 2807 | |
| 2808 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2809 | { |
| 2810 | if(saturate) |
| 2811 | { |
| 2812 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2813 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2814 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2815 | // PackUnsigned takes care of 0x0000 saturation. |
| 2816 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2817 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2818 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2819 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2820 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2821 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2822 | Int4 int4(cast); |
| 2823 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2824 | } |
| 2825 | else |
| 2826 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2827 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2828 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2829 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2830 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2831 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2832 | *this = Short4(Int4(cast)); |
| 2833 | } |
| 2834 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2835 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2836 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2837 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2838 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2842 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2843 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2847 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2848 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2849 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2850 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2851 | { |
| 2852 | UShort4 result; |
| 2853 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2854 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2855 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2856 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2857 | |
| 2858 | return result; |
| 2859 | } |
| 2860 | else |
| 2861 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2862 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2867 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2868 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2869 | if(emulateIntrinsics) |
| 2870 | { |
| 2871 | UShort4 result; |
| 2872 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2873 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2874 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2875 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2876 | |
| 2877 | return result; |
| 2878 | } |
| 2879 | else |
| 2880 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2881 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2886 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2887 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2888 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2889 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2890 | ::basicBlock->appendInst(cmp); |
| 2891 | |
| 2892 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2893 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2894 | ::basicBlock->appendInst(select); |
| 2895 | |
| 2896 | return RValue<UShort4>(V(result)); |
| 2897 | } |
| 2898 | |
| 2899 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2900 | { |
| 2901 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2902 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2903 | ::basicBlock->appendInst(cmp); |
| 2904 | |
| 2905 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2906 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2907 | ::basicBlock->appendInst(select); |
| 2908 | |
| 2909 | return RValue<UShort4>(V(result)); |
| 2910 | } |
| 2911 | |
| 2912 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 2913 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2914 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2915 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2916 | } |
| 2917 | |
| 2918 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2919 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2920 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2921 | if(emulateIntrinsics) |
| 2922 | { |
| 2923 | UShort4 result; |
| 2924 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2925 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2926 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2927 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2928 | |
| 2929 | return result; |
| 2930 | } |
| 2931 | else |
| 2932 | { |
| 2933 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2934 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2935 | auto paddusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2936 | paddusw->addArg(x.value()); |
| 2937 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2938 | ::basicBlock->appendInst(paddusw); |
| 2939 | |
| 2940 | return RValue<UShort4>(V(result)); |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2945 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2946 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2947 | if(emulateIntrinsics) |
| 2948 | { |
| 2949 | UShort4 result; |
| 2950 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2951 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2952 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2953 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2954 | |
| 2955 | return result; |
| 2956 | } |
| 2957 | else |
| 2958 | { |
| 2959 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2960 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2961 | auto psubusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2962 | psubusw->addArg(x.value()); |
| 2963 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2964 | ::basicBlock->appendInst(psubusw); |
| 2965 | |
| 2966 | return RValue<UShort4>(V(result)); |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2971 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2972 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2973 | if(emulateIntrinsics) |
| 2974 | { |
| 2975 | UShort4 result; |
| 2976 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2977 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2978 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2979 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 2980 | |
| 2981 | return result; |
| 2982 | } |
| 2983 | else |
| 2984 | { |
| 2985 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2986 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 2987 | auto pmulhuw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2988 | pmulhuw->addArg(x.value()); |
| 2989 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2990 | ::basicBlock->appendInst(pmulhuw); |
| 2991 | |
| 2992 | return RValue<UShort4>(V(result)); |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2997 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2998 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2999 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3000 | |
| 3001 | // Scalarized implementation. |
| 3002 | Int4 result; |
| 3003 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 3004 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 3005 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 3006 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 3007 | |
| 3008 | return result; |
| 3009 | } |
| 3010 | |
| 3011 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3012 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3013 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3014 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3015 | |
| 3016 | if(false) // Partial product based implementation. |
| 3017 | { |
| 3018 | auto xh = x >> 16; |
| 3019 | auto yh = y >> 16; |
| 3020 | auto xl = x & UInt4(0x0000FFFF); |
| 3021 | auto yl = y & UInt4(0x0000FFFF); |
| 3022 | auto xlyh = xl * yh; |
| 3023 | auto xhyl = xh * yl; |
| 3024 | auto xlyhh = xlyh >> 16; |
| 3025 | auto xhylh = xhyl >> 16; |
| 3026 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3027 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3028 | auto xlylh = (xl * yl) >> 16; |
| 3029 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3030 | |
| 3031 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3032 | } |
| 3033 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3034 | // Scalarized implementation. |
| 3035 | Int4 result; |
| 3036 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3037 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3038 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3039 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3040 | |
| 3041 | return As<UInt4>(result); |
| 3042 | } |
| 3043 | |
| 3044 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3045 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3046 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3047 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3048 | return UShort4(0); |
| 3049 | } |
| 3050 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3051 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3052 | { |
| 3053 | return T(Type_v4i16); |
| 3054 | } |
| 3055 | |
| 3056 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 3057 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3058 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3059 | return RValue<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3063 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3064 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3065 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3069 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3071 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3072 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3073 | Short8 result; |
| 3074 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3075 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3076 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3077 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3078 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3079 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3080 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3081 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3082 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3083 | return result; |
| 3084 | } |
| 3085 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3086 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3087 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3088 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3089 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3090 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3091 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 3092 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3093 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3094 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3095 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3096 | Short8 result; |
| 3097 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 3098 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 3099 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 3100 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 3101 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 3102 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 3103 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 3104 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3105 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3106 | return result; |
| 3107 | } |
| 3108 | else |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3109 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3110 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3111 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3112 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3113 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3114 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3115 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3116 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3117 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3118 | return Int4(0); |
| 3119 | } |
| 3120 | |
| 3121 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 3122 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3123 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3124 | UNIMPLEMENTED_NO_BUG("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3125 | return Short8(0); |
| 3126 | } |
| 3127 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3128 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3129 | { |
| 3130 | return T(Ice::IceType_v8i16); |
| 3131 | } |
| 3132 | |
| 3133 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 3134 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3135 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3136 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3140 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3141 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3142 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3146 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3147 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3148 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3149 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3150 | UShort8 result; |
| 3151 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3152 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3153 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3154 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3155 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3156 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3157 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3158 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3159 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3160 | return result; |
| 3161 | } |
| 3162 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3163 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3164 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3165 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3166 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3167 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3168 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 3169 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3170 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3171 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3172 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3173 | UShort8 result; |
| 3174 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 3175 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 3176 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 3177 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 3178 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 3179 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 3180 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 3181 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3182 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3183 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3184 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3185 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3186 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3187 | return RValue<UShort8>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3188 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3189 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3190 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3191 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3192 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3193 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3194 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3195 | return UShort8(0); |
| 3196 | } |
| 3197 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3198 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3199 | { |
| 3200 | return T(Ice::IceType_v8i16); |
| 3201 | } |
| 3202 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3203 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3204 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3205 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3206 | RValue<Int> res = val; |
| 3207 | val += 1; |
| 3208 | return res; |
| 3209 | } |
| 3210 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3211 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3212 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3213 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3214 | val += 1; |
| 3215 | return val; |
| 3216 | } |
| 3217 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3218 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3219 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3220 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3221 | RValue<Int> res = val; |
| 3222 | val -= 1; |
| 3223 | return res; |
| 3224 | } |
| 3225 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3226 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3227 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3228 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3229 | val -= 1; |
| 3230 | return val; |
| 3231 | } |
| 3232 | |
| 3233 | RValue<Int> RoundInt(RValue<Float> cast) |
| 3234 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3235 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3236 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3237 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3238 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3239 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3240 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3241 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3242 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3243 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3244 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3245 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3246 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3247 | ::basicBlock->appendInst(nearbyint); |
| 3248 | |
| 3249 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3250 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3251 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3252 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3253 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3254 | { |
| 3255 | return T(Ice::IceType_i32); |
| 3256 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3257 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3258 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3259 | { |
| 3260 | return T(Ice::IceType_i64); |
| 3261 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3262 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3263 | UInt::UInt(RValue<Float> cast) |
| 3264 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3265 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3266 | // Smallest positive value representable in UInt, but not in Int |
| 3267 | const unsigned int ustart = 0x80000000u; |
| 3268 | const float ustartf = float(ustart); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3269 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3270 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3271 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3272 | // Check if the value can be represented as an Int |
| 3273 | IfThenElse(cast >= ustartf, |
| 3274 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3275 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3276 | // Otherwise, just convert normally |
| 3277 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3278 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3279 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3280 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3281 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3282 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3283 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3284 | RValue<UInt> res = val; |
| 3285 | val += 1; |
| 3286 | return res; |
| 3287 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3288 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3289 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3290 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3291 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3292 | val += 1; |
| 3293 | return val; |
| 3294 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3295 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3296 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3297 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3299 | RValue<UInt> res = val; |
| 3300 | val -= 1; |
| 3301 | return res; |
| 3302 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3303 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3304 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3305 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3306 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3307 | val -= 1; |
| 3308 | return val; |
| 3309 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3310 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3311 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3312 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3313 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3314 | // } |
| 3315 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3316 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3317 | { |
| 3318 | return T(Ice::IceType_i32); |
| 3319 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3320 | |
| 3321 | // Int2::Int2(RValue<Int> cast) |
| 3322 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3323 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3324 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3325 | // |
| 3326 | // Constant *shuffle[2]; |
| 3327 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3328 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3329 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3330 | // Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::type()), Nucleus::createConstantVector(shuffle, 2)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3331 | // |
| 3332 | // storeValue(replicate); |
| 3333 | // } |
| 3334 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3335 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3336 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3337 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3338 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3339 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3340 | Int2 result; |
| 3341 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3342 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3343 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3344 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3345 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3346 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3347 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3348 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3349 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3350 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3351 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3352 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3353 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3355 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3356 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3357 | Int2 result; |
| 3358 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3359 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3360 | |
| 3361 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3362 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3363 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3364 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3365 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3366 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3367 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3368 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3369 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3370 | { |
| 3371 | return T(Type_v2i32); |
| 3372 | } |
| 3373 | |
| 3374 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 3375 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3376 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3377 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3378 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3379 | UInt2 result; |
| 3380 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3381 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3382 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3383 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3384 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3385 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3386 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3387 | return RValue<UInt2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3388 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3389 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3390 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3391 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3392 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3393 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3394 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3395 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3396 | UInt2 result; |
| 3397 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3398 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3399 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3400 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3401 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3402 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3403 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3404 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3405 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3406 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3407 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3408 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3409 | { |
| 3410 | return T(Type_v2i32); |
| 3411 | } |
| 3412 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3413 | Int4::Int4(RValue<Byte4> cast) |
| 3414 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3415 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3416 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3417 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3418 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3419 | |
| 3420 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3421 | int swizzle[16] = { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3422 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3423 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3424 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3425 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3426 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3427 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3428 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3429 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3430 | storeValue(f); |
| 3431 | } |
| 3432 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3433 | Int4::Int4(RValue<SByte4> cast) |
| 3434 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3435 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3436 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3437 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3438 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3439 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3440 | int swizzle[16] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3441 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3442 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3443 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3444 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3445 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3446 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3447 | |
| 3448 | *this = As<Int4>(e) >> 24; |
| 3449 | } |
| 3450 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3451 | Int4::Int4(RValue<Short4> cast) |
| 3452 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3453 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3454 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3455 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3456 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3457 | |
| 3458 | *this = As<Int4>(c) >> 16; |
| 3459 | } |
| 3460 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3461 | Int4::Int4(RValue<UShort4> cast) |
| 3462 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3463 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3464 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3465 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3466 | Value *c = Nucleus::createShuffleVector(cast.value(), Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3467 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3468 | storeValue(d); |
| 3469 | } |
| 3470 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3471 | Int4::Int4(RValue<Int> rhs) |
| 3472 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3473 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3474 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3475 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3476 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3477 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3478 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3479 | |
| 3480 | storeValue(replicate); |
| 3481 | } |
| 3482 | |
| 3483 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3484 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3485 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3486 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3487 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3488 | Int4 result; |
| 3489 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3490 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3491 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3492 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3493 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3494 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3495 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3496 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3497 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3498 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3499 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3500 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3501 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3502 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3503 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3504 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3505 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3506 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3507 | Int4 result; |
| 3508 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3509 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3510 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3511 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3512 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3513 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3514 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3515 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3516 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3517 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3518 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3519 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3520 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3521 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3522 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3523 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3524 | return RValue<Int4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3528 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3529 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3530 | return RValue<Int4>(Nucleus::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3534 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3535 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3536 | return RValue<Int4>(Nucleus::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3540 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3541 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3542 | return RValue<Int4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3546 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3547 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3548 | return RValue<Int4>(Nucleus::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3552 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3553 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3554 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3555 | } |
| 3556 | |
Nicolas Capens | 629bf95 | 2022-01-18 15:08:14 -0500 | [diff] [blame] | 3557 | RValue<Int4> Abs(RValue<Int4> x) |
| 3558 | { |
| 3559 | // TODO: Optimize. |
| 3560 | auto negative = x >> 31; |
| 3561 | return (x ^ negative) - negative; |
| 3562 | } |
| 3563 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3564 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3565 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3566 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3567 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3568 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3569 | ::basicBlock->appendInst(cmp); |
| 3570 | |
| 3571 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3572 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3573 | ::basicBlock->appendInst(select); |
| 3574 | |
| 3575 | return RValue<Int4>(V(result)); |
| 3576 | } |
| 3577 | |
| 3578 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3579 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3580 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3581 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3582 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3583 | ::basicBlock->appendInst(cmp); |
| 3584 | |
| 3585 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3586 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3587 | ::basicBlock->appendInst(select); |
| 3588 | |
| 3589 | return RValue<Int4>(V(result)); |
| 3590 | } |
| 3591 | |
| 3592 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3593 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3594 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3595 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3596 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3597 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3598 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3599 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3600 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3601 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3602 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3603 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3604 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3605 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3606 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3607 | |
| 3608 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3609 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3610 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3611 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3612 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3613 | { |
| 3614 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3615 | |
| 3616 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3617 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3618 | // saturate to 0x80000000. |
| 3619 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3620 | |
| 3621 | if(emulateIntrinsics || CPUID::ARM) |
| 3622 | { |
| 3623 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3624 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3625 | } |
| 3626 | else |
| 3627 | { |
| 3628 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3629 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3630 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3631 | nearbyint->addArg(clamped.value()); |
| 3632 | ::basicBlock->appendInst(nearbyint); |
| 3633 | |
| 3634 | return RValue<Int4>(V(result)); |
| 3635 | } |
| 3636 | } |
| 3637 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3638 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3639 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3640 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3641 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3642 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3643 | Short8 result; |
| 3644 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3645 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3646 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3647 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3648 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3649 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3650 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3651 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3652 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3653 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3654 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3655 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3656 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3657 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3658 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3659 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3660 | pack->addArg(x.value()); |
| 3661 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3662 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3663 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3664 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3665 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3666 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3667 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3668 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3669 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3670 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3671 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3672 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3673 | RValue<Int4> sx = As<Int4>(x); |
| 3674 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3675 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3676 | RValue<Int4> sy = As<Int4>(y); |
| 3677 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3678 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3679 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3680 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3681 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3682 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3683 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3684 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3685 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3686 | pack->addArg(x.value()); |
| 3687 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3688 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3689 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3690 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3691 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3692 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3693 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3694 | RValue<Int> SignMask(RValue<Int4> x) |
| 3695 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3696 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3697 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3698 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3699 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3700 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3701 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3702 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3703 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3704 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3705 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3706 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3707 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3708 | ::basicBlock->appendInst(movmsk); |
| 3709 | |
| 3710 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3711 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3712 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3713 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3714 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3715 | { |
| 3716 | return T(Ice::IceType_v4i32); |
| 3717 | } |
| 3718 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3719 | UInt4::UInt4(RValue<Float4> cast) |
| 3720 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3721 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3722 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3723 | // Smallest positive value representable in UInt, but not in Int |
| 3724 | const unsigned int ustart = 0x80000000u; |
| 3725 | const float ustartf = float(ustart); |
| 3726 | |
| 3727 | // Check if the value can be represented as an Int |
| 3728 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3729 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3730 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3731 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3732 | (~uiValue & Int4(cast)); |
| 3733 | // If the value is negative, store 0, otherwise store the result of the conversion |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3734 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3735 | } |
| 3736 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3737 | UInt4::UInt4(RValue<UInt> rhs) |
| 3738 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3739 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3740 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3741 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3742 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3743 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3744 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3745 | |
| 3746 | storeValue(replicate); |
| 3747 | } |
| 3748 | |
| 3749 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3750 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3751 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3752 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3753 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3754 | UInt4 result; |
| 3755 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3756 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3757 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3758 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3759 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3760 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3761 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3762 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3763 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3764 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3765 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3766 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3767 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3768 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3769 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3770 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3771 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3772 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3773 | UInt4 result; |
| 3774 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3775 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3776 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3777 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3778 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3779 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3780 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3781 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3782 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3783 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3784 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3785 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3786 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3787 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3788 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3789 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3790 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3794 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3795 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3796 | return RValue<UInt4>(Nucleus::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3800 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3801 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3802 | return RValue<UInt4>(Nucleus::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3806 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3807 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3808 | return RValue<UInt4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3812 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3813 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3814 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3818 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3819 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3820 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3824 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3825 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3826 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3827 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3828 | ::basicBlock->appendInst(cmp); |
| 3829 | |
| 3830 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3831 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3832 | ::basicBlock->appendInst(select); |
| 3833 | |
| 3834 | return RValue<UInt4>(V(result)); |
| 3835 | } |
| 3836 | |
| 3837 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3838 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3839 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3840 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3841 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3842 | ::basicBlock->appendInst(cmp); |
| 3843 | |
| 3844 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3845 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3846 | ::basicBlock->appendInst(select); |
| 3847 | |
| 3848 | return RValue<UInt4>(V(result)); |
| 3849 | } |
| 3850 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3851 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3852 | { |
| 3853 | return T(Ice::IceType_v4i32); |
| 3854 | } |
| 3855 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3856 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3857 | { |
| 3858 | return T(Ice::IceType_i16); |
| 3859 | } |
| 3860 | |
| 3861 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3862 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3863 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3864 | return 1.0f / x; |
| 3865 | } |
| 3866 | |
| 3867 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3868 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3869 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3870 | return Rcp_pp(Sqrt(x)); |
| 3871 | } |
| 3872 | |
| 3873 | RValue<Float> Sqrt(RValue<Float> x) |
| 3874 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3875 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3876 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3877 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 3878 | auto sqrt = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3879 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3880 | ::basicBlock->appendInst(sqrt); |
| 3881 | |
| 3882 | return RValue<Float>(V(result)); |
| 3883 | } |
| 3884 | |
| 3885 | RValue<Float> Round(RValue<Float> x) |
| 3886 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3887 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3888 | return Float4(Round(Float4(x))).x; |
| 3889 | } |
| 3890 | |
| 3891 | RValue<Float> Trunc(RValue<Float> x) |
| 3892 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3893 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3894 | return Float4(Trunc(Float4(x))).x; |
| 3895 | } |
| 3896 | |
| 3897 | RValue<Float> Frac(RValue<Float> x) |
| 3898 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3899 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3900 | return Float4(Frac(Float4(x))).x; |
| 3901 | } |
| 3902 | |
| 3903 | RValue<Float> Floor(RValue<Float> x) |
| 3904 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3905 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3906 | return Float4(Floor(Float4(x))).x; |
| 3907 | } |
| 3908 | |
| 3909 | RValue<Float> Ceil(RValue<Float> x) |
| 3910 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3911 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3912 | return Float4(Ceil(Float4(x))).x; |
| 3913 | } |
| 3914 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3915 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3916 | { |
| 3917 | return T(Ice::IceType_f32); |
| 3918 | } |
| 3919 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3920 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3921 | { |
| 3922 | return T(Type_v2f32); |
| 3923 | } |
| 3924 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3925 | Float4::Float4(RValue<Float> rhs) |
| 3926 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3927 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3928 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3929 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3930 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3931 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3932 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3933 | |
| 3934 | storeValue(replicate); |
| 3935 | } |
| 3936 | |
Nicolas Capens | bc74bc2 | 2022-01-26 10:47:00 -0500 | [diff] [blame^] | 3937 | RValue<Float4> MulAdd(RValue<Float4> x, RValue<Float4> y, RValue<Float4> z) |
| 3938 | { |
| 3939 | // TODO(b/214591655): Use FMA when available. |
| 3940 | return x * y + z; |
| 3941 | } |
| 3942 | |
Nicolas Capens | 629bf95 | 2022-01-18 15:08:14 -0500 | [diff] [blame] | 3943 | RValue<Float4> Abs(RValue<Float4> x) |
| 3944 | { |
| 3945 | // TODO: Optimize. |
| 3946 | Value *vector = Nucleus::createBitCast(x.value(), Int4::type()); |
| 3947 | int64_t constantVector[4] = { 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF }; |
| 3948 | Value *result = Nucleus::createAnd(vector, Nucleus::createConstantVector(constantVector, Int4::type())); |
| 3949 | |
| 3950 | return As<Float4>(result); |
| 3951 | } |
| 3952 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3953 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3954 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3955 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3956 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3957 | auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3958 | ::basicBlock->appendInst(cmp); |
| 3959 | |
| 3960 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3961 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3962 | ::basicBlock->appendInst(select); |
| 3963 | |
| 3964 | return RValue<Float4>(V(result)); |
| 3965 | } |
| 3966 | |
| 3967 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3968 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3969 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3970 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3971 | auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3972 | ::basicBlock->appendInst(cmp); |
| 3973 | |
| 3974 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3975 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3976 | ::basicBlock->appendInst(select); |
| 3977 | |
| 3978 | return RValue<Float4>(V(result)); |
| 3979 | } |
| 3980 | |
| 3981 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3982 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3983 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3984 | return Float4(1.0f) / x; |
| 3985 | } |
| 3986 | |
| 3987 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3988 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3989 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3990 | return Rcp_pp(Sqrt(x)); |
| 3991 | } |
| 3992 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 3993 | bool HasRcpApprox() |
| 3994 | { |
| 3995 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3996 | return false; |
| 3997 | } |
| 3998 | |
| 3999 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 4000 | { |
| 4001 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4002 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 4003 | return { 0.0f }; |
| 4004 | } |
| 4005 | |
| 4006 | RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2) |
| 4007 | { |
| 4008 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4009 | UNREACHABLE("RValue<Float> RcpApprox()"); |
| 4010 | return { 0.0f }; |
| 4011 | } |
| 4012 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 4013 | bool HasRcpSqrtApprox() |
| 4014 | { |
| 4015 | return false; |
| 4016 | } |
| 4017 | |
| 4018 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 4019 | { |
| 4020 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4021 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 4022 | return { 0.0f }; |
| 4023 | } |
| 4024 | |
| 4025 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 4026 | { |
| 4027 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4028 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 4029 | return { 0.0f }; |
| 4030 | } |
| 4031 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4032 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4033 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4034 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4035 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4036 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4037 | Float4 result; |
| 4038 | result.x = Sqrt(Float(Float4(x).x)); |
| 4039 | result.y = Sqrt(Float(Float4(x).y)); |
| 4040 | result.z = Sqrt(Float(Float4(x).z)); |
| 4041 | result.w = Sqrt(Float(Float4(x).w)); |
| 4042 | |
| 4043 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4044 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4045 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4046 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4047 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4048 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4049 | auto sqrt = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4050 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4051 | ::basicBlock->appendInst(sqrt); |
| 4052 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4053 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4054 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4055 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4056 | |
| 4057 | RValue<Int> SignMask(RValue<Float4> x) |
| 4058 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4059 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4060 | if(emulateIntrinsics || CPUID::ARM) |
| 4061 | { |
| 4062 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4063 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4064 | } |
| 4065 | else |
| 4066 | { |
| 4067 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4068 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4069 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4070 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4071 | ::basicBlock->appendInst(movmsk); |
| 4072 | |
| 4073 | return RValue<Int>(V(result)); |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 4078 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4079 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4080 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 4084 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4085 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4086 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 4090 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4091 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4092 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4096 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4097 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4098 | return RValue<Int4>(Nucleus::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 4102 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4103 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4104 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 4108 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4109 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4110 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 4114 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4115 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4116 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 4120 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4121 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4122 | return RValue<Int4>(Nucleus::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 4126 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4127 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4128 | return RValue<Int4>(Nucleus::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4132 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4133 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4134 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4138 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4139 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4140 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4144 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4145 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4146 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | RValue<Float4> Round(RValue<Float4> x) |
| 4150 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4151 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4152 | if(emulateIntrinsics || CPUID::ARM) |
| 4153 | { |
| 4154 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4155 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4156 | } |
| 4157 | else if(CPUID::SSE4_1) |
| 4158 | { |
| 4159 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4160 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4161 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4162 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4163 | round->addArg(::context->getConstantInt32(0)); |
| 4164 | ::basicBlock->appendInst(round); |
| 4165 | |
| 4166 | return RValue<Float4>(V(result)); |
| 4167 | } |
| 4168 | else |
| 4169 | { |
| 4170 | return Float4(RoundInt(x)); |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | RValue<Float4> Trunc(RValue<Float4> x) |
| 4175 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4176 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4177 | if(CPUID::SSE4_1) |
| 4178 | { |
| 4179 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4180 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4181 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4182 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4183 | round->addArg(::context->getConstantInt32(3)); |
| 4184 | ::basicBlock->appendInst(round); |
| 4185 | |
| 4186 | return RValue<Float4>(V(result)); |
| 4187 | } |
| 4188 | else |
| 4189 | { |
| 4190 | return Float4(Int4(x)); |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | RValue<Float4> Frac(RValue<Float4> x) |
| 4195 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4196 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4197 | Float4 frc; |
| 4198 | |
| 4199 | if(CPUID::SSE4_1) |
| 4200 | { |
| 4201 | frc = x - Floor(x); |
| 4202 | } |
| 4203 | else |
| 4204 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4205 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4206 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4207 | frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1))); // Add 1.0 if negative. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | // x - floor(x) can be 1.0 for very small negative x. |
| 4211 | // Clamp against the value just below 1.0. |
| 4212 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4213 | } |
| 4214 | |
| 4215 | RValue<Float4> Floor(RValue<Float4> x) |
| 4216 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4217 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4218 | if(CPUID::SSE4_1) |
| 4219 | { |
| 4220 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4221 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4222 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4223 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4224 | round->addArg(::context->getConstantInt32(1)); |
| 4225 | ::basicBlock->appendInst(round); |
| 4226 | |
| 4227 | return RValue<Float4>(V(result)); |
| 4228 | } |
| 4229 | else |
| 4230 | { |
| 4231 | return x - Frac(x); |
| 4232 | } |
| 4233 | } |
| 4234 | |
| 4235 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4236 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4237 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4238 | if(CPUID::SSE4_1) |
| 4239 | { |
| 4240 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4241 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4242 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4243 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4244 | round->addArg(::context->getConstantInt32(2)); |
| 4245 | ::basicBlock->appendInst(round); |
| 4246 | |
| 4247 | return RValue<Float4>(V(result)); |
| 4248 | } |
| 4249 | else |
| 4250 | { |
| 4251 | return -Floor(-x); |
| 4252 | } |
| 4253 | } |
| 4254 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4255 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4256 | { |
| 4257 | return T(Ice::IceType_v4f32); |
| 4258 | } |
| 4259 | |
| 4260 | RValue<Long> Ticks() |
| 4261 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4263 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4264 | return Long(Int(0)); |
| 4265 | } |
| 4266 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4267 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4268 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4269 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4270 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4271 | } |
| 4272 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4273 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4274 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4275 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4276 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4277 | } |
| 4278 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4279 | Value *Call(RValue<Pointer<Byte>> fptr, Type *retTy, std::initializer_list<Value *> args, std::initializer_list<Type *> argTys) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4280 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4281 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4282 | return V(sz::Call(::function, ::basicBlock, T(retTy), V(fptr.value()), V(args), false)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | void Breakpoint() |
| 4286 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4287 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4288 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Trap, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4289 | auto trap = Ice::InstIntrinsic::create(::function, 0, nullptr, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4290 | ::basicBlock->appendInst(trap); |
| 4291 | } |
| 4292 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4293 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4294 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4295 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4296 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicFence, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4297 | auto inst = Ice::InstIntrinsic::create(::function, 0, nullptr, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4298 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4299 | inst->addArg(order); |
| 4300 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4301 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4302 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4303 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4304 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4305 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame] | 4306 | UNIMPLEMENTED("b/155867273 Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4307 | return nullptr; |
| 4308 | } |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame] | 4309 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4310 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4311 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame] | 4313 | UNIMPLEMENTED("b/155867273 Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4314 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4315 | |
| 4316 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4317 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4318 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4319 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4320 | } |
| 4321 | |
| 4322 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4323 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4324 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4325 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4326 | } |
| 4327 | |
| 4328 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4329 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4330 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4331 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4332 | } |
| 4333 | |
| 4334 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4335 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4337 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4338 | } |
| 4339 | |
| 4340 | RValue<Float> Exp2(RValue<Float> x) |
| 4341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4343 | return emulated::Exp2(x); |
| 4344 | } |
| 4345 | |
| 4346 | RValue<Float> Log2(RValue<Float> x) |
| 4347 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4348 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4349 | return emulated::Log2(x); |
| 4350 | } |
| 4351 | |
| 4352 | RValue<Float4> Sin(RValue<Float4> x) |
| 4353 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4355 | return optimal::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | RValue<Float4> Cos(RValue<Float4> x) |
| 4359 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4361 | return optimal::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | RValue<Float4> Tan(RValue<Float4> x) |
| 4365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4367 | return optimal::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4368 | } |
| 4369 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4370 | RValue<Float4> Asin(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4371 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4372 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4373 | if(p == Precision::Full) |
| 4374 | { |
| 4375 | return emulated::Asin(x); |
| 4376 | } |
| 4377 | return optimal::Asin_8_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4378 | } |
| 4379 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4380 | RValue<Float4> Acos(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4381 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4382 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4383 | // Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial approximation |
| 4384 | // version of acos, unlike for Asin, which requires higher precision algorithms. |
| 4385 | return optimal::Acos_4_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | RValue<Float4> Atan(RValue<Float4> x) |
| 4389 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4390 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4391 | return optimal::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4395 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4396 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4397 | return optimal::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4398 | } |
| 4399 | |
| 4400 | RValue<Float4> Cosh(RValue<Float4> x) |
| 4401 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4402 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4403 | return optimal::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | RValue<Float4> Tanh(RValue<Float4> x) |
| 4407 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4408 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4409 | return optimal::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | RValue<Float4> Asinh(RValue<Float4> x) |
| 4413 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4415 | return optimal::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | RValue<Float4> Acosh(RValue<Float4> x) |
| 4419 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4421 | return optimal::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4427 | return optimal::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 4431 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4432 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4433 | return optimal::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4437 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4438 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4439 | return optimal::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | RValue<Float4> Exp(RValue<Float4> x) |
| 4443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4445 | return optimal::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | RValue<Float4> Log(RValue<Float4> x) |
| 4449 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4450 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4451 | return optimal::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4455 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4457 | return optimal::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | RValue<Float4> Log2(RValue<Float4> x) |
| 4461 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4463 | return optimal::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4467 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4468 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4469 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4470 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4471 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4472 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4473 | } |
| 4474 | else |
| 4475 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4476 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4477 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Ctlz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4478 | auto ctlz = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4479 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4480 | ::basicBlock->appendInst(ctlz); |
| 4481 | |
| 4482 | return RValue<UInt>(V(result)); |
| 4483 | } |
| 4484 | } |
| 4485 | |
| 4486 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4487 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4488 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4489 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4490 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4491 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4492 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4493 | } |
| 4494 | else |
| 4495 | { |
| 4496 | // TODO: implement vectorized version in Subzero |
| 4497 | UInt4 result; |
| 4498 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4499 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4500 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4501 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4502 | return result; |
| 4503 | } |
| 4504 | } |
| 4505 | |
| 4506 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4507 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4508 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4509 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4510 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4511 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4512 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4513 | } |
| 4514 | else |
| 4515 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4516 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4517 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Cttz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 4518 | auto ctlz = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4519 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4520 | ::basicBlock->appendInst(ctlz); |
| 4521 | |
| 4522 | return RValue<UInt>(V(result)); |
| 4523 | } |
| 4524 | } |
| 4525 | |
| 4526 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4527 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4528 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4529 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4530 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4531 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4532 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4533 | } |
| 4534 | else |
| 4535 | { |
| 4536 | // TODO: implement vectorized version in Subzero |
| 4537 | UInt4 result; |
| 4538 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4539 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4540 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4541 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4542 | return result; |
| 4543 | } |
| 4544 | } |
| 4545 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4546 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4547 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4548 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4549 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4550 | } |
| 4551 | |
| 4552 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4553 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4554 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4555 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4556 | } |
| 4557 | |
| 4558 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4559 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4560 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4561 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4562 | } |
| 4563 | |
| 4564 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4565 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4566 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4567 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4568 | } |
| 4569 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4570 | void EmitDebugLocation() |
| 4571 | { |
| 4572 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4573 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4574 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4575 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4576 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4577 | void FlushDebug() {} |
| 4578 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4579 | namespace { |
| 4580 | namespace coro { |
| 4581 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4582 | // Instance data per generated coroutine |
| 4583 | // This is the "handle" type used for Coroutine functions |
| 4584 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4585 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4586 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4587 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4588 | bool done = false; // the coroutine should stop at the next yield() |
| 4589 | bool terminated = false; // the coroutine has finished. |
| 4590 | bool inRoutine = false; // is the coroutine currently executing? |
| 4591 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4592 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4593 | void *promisePtr = nullptr; |
| 4594 | }; |
| 4595 | |
| 4596 | CoroutineData *createCoroutineData() |
| 4597 | { |
| 4598 | return new CoroutineData{}; |
| 4599 | } |
| 4600 | |
| 4601 | void destroyCoroutineData(CoroutineData *coroData) |
| 4602 | { |
| 4603 | delete coroData; |
| 4604 | } |
| 4605 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4606 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4607 | // caller's call to await(). |
| 4608 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4609 | // is called. |
| 4610 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4611 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4612 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4613 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4614 | ASSERT(coroData->inRoutine); |
| 4615 | coroData->inRoutine = false; |
| 4616 | coroData->mainFiber->notify(); |
| 4617 | while(!coroData->inRoutine) |
| 4618 | { |
| 4619 | coroData->routineFiber->wait(); |
| 4620 | } |
| 4621 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4622 | } |
| 4623 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4624 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4625 | // or the coroutine terminates. |
| 4626 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4627 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4628 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4629 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4630 | ASSERT(!coroData->inRoutine); |
| 4631 | coroData->inRoutine = true; |
| 4632 | coroData->routineFiber->notify(); |
| 4633 | while(coroData->inRoutine) |
| 4634 | { |
| 4635 | coroData->mainFiber->wait(); |
| 4636 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4637 | } |
| 4638 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4639 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4640 | // until the coroutine ends, and deletes the coroutine data. |
| 4641 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4642 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4643 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4644 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4645 | ASSERT(!coroData->inRoutine); |
| 4646 | if(!coroData->terminated) |
| 4647 | { |
| 4648 | coroData->done = true; |
| 4649 | coroData->inRoutine = true; |
| 4650 | coroData->routineFiber->notify(); |
| 4651 | while(!coroData->terminated) |
| 4652 | { |
| 4653 | coroData->mainFiber->wait(); |
| 4654 | } |
| 4655 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4656 | if(coroData->useInternalScheduler) |
| 4657 | { |
| 4658 | ::getOrCreateScheduler().unbind(); |
| 4659 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4660 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | namespace detail { |
| 4664 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4665 | } // namespace detail |
| 4666 | |
| 4667 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4668 | { |
| 4669 | ASSERT(!detail::coroHandle); |
| 4670 | detail::coroHandle = handle; |
| 4671 | } |
| 4672 | |
| 4673 | Nucleus::CoroutineHandle getHandleParam() |
| 4674 | { |
| 4675 | ASSERT(detail::coroHandle); |
| 4676 | auto handle = detail::coroHandle; |
| 4677 | detail::coroHandle = {}; |
| 4678 | return handle; |
| 4679 | } |
| 4680 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4681 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4682 | { |
| 4683 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4684 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4688 | { |
| 4689 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4690 | coroData->promisePtr = promisePtr; |
| 4691 | } |
| 4692 | |
| 4693 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4694 | { |
| 4695 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4696 | return coroData->promisePtr; |
| 4697 | } |
| 4698 | |
| 4699 | } // namespace coro |
| 4700 | } // namespace |
| 4701 | |
| 4702 | // Used to generate coroutines. |
| 4703 | // Lifetime: from yield to acquireCoroutine |
| 4704 | class CoroutineGenerator |
| 4705 | { |
| 4706 | public: |
| 4707 | CoroutineGenerator() |
| 4708 | { |
| 4709 | } |
| 4710 | |
| 4711 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4712 | void generateCoroutineBegin() |
| 4713 | { |
| 4714 | // Begin building the main coroutine_begin() function. |
| 4715 | // We insert these instructions at the top of the entry node, |
| 4716 | // before existing reactor-generated instructions. |
| 4717 | |
| 4718 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4719 | // { |
| 4720 | // this->handle = coro::getHandleParam(); |
| 4721 | // |
| 4722 | // YieldType promise; |
| 4723 | // coro::setPromisePtr(handle, &promise); // For await |
| 4724 | // |
| 4725 | // ... <REACTOR CODE> ... |
| 4726 | // |
| 4727 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4728 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4729 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4730 | |
| 4731 | // YieldType promise; |
| 4732 | // coro::setPromisePtr(handle, &promise); // For await |
| 4733 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4734 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4738 | void generateYield(Value *val) |
| 4739 | { |
| 4740 | // ... <REACTOR CODE> ... |
| 4741 | // |
| 4742 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4743 | // if (!coro::suspend(handle)) { |
| 4744 | // return false; // coroutine has been stopped by the caller. |
| 4745 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4746 | // |
| 4747 | // ... <REACTOR CODE> ... |
| 4748 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4749 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4750 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4751 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4752 | // if (!coro::suspend(handle)) { |
| 4753 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4754 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4755 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4756 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4757 | |
| 4758 | // return false; // coroutine has been stopped by the caller. |
| 4759 | ::basicBlock = doneBlock; |
| 4760 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4761 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4762 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4763 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4767 | |
| 4768 | // Generates the await function for the current coroutine. |
| 4769 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4770 | static FunctionUniquePtr generateAwaitFunction() |
| 4771 | { |
| 4772 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4773 | // { |
| 4774 | // if (coro::isDone()) |
| 4775 | // { |
| 4776 | // return false; |
| 4777 | // } |
| 4778 | // else // resume |
| 4779 | // { |
| 4780 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4781 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4782 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4783 | // return true; |
| 4784 | // } |
| 4785 | // } |
| 4786 | |
| 4787 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4788 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4789 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4790 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4791 | |
| 4792 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4793 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4794 | |
| 4795 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4796 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4797 | |
| 4798 | auto doneBlock = awaitFunc->makeNode(); |
| 4799 | { |
| 4800 | // return false; |
| 4801 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4802 | doneBlock->appendInst(ret); |
| 4803 | } |
| 4804 | |
| 4805 | auto resumeBlock = awaitFunc->makeNode(); |
| 4806 | { |
| 4807 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4808 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4809 | |
| 4810 | // *out = *promise; |
| 4811 | // Load promise value |
| 4812 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4813 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4814 | resumeBlock->appendInst(load); |
| 4815 | // Then store it in output param |
| 4816 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4817 | resumeBlock->appendInst(store); |
| 4818 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4819 | // coro::resume(handle); |
| 4820 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4821 | |
| 4822 | // return true; |
| 4823 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4824 | resumeBlock->appendInst(ret); |
| 4825 | } |
| 4826 | |
| 4827 | // if (coro::isDone()) |
| 4828 | // { |
| 4829 | // <doneBlock> |
| 4830 | // } |
| 4831 | // else // resume |
| 4832 | // { |
| 4833 | // <resumeBlock> |
| 4834 | // } |
| 4835 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4836 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4837 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4838 | bb->appendInst(br); |
| 4839 | |
| 4840 | return FunctionUniquePtr{ awaitFunc }; |
| 4841 | } |
| 4842 | |
| 4843 | // Generates the destroy function for the current coroutine. |
| 4844 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4845 | static FunctionUniquePtr generateDestroyFunction() |
| 4846 | { |
| 4847 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4848 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4849 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4850 | // return; |
| 4851 | // } |
| 4852 | |
| 4853 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4854 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4855 | |
| 4856 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4857 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4858 | |
| 4859 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4860 | |
| 4861 | auto *bb = destroyFunc->getEntryNode(); |
| 4862 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4863 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4864 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4865 | |
| 4866 | // return; |
| 4867 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4868 | bb->appendInst(ret); |
| 4869 | |
| 4870 | return FunctionUniquePtr{ destroyFunc }; |
| 4871 | } |
| 4872 | |
| 4873 | private: |
| 4874 | Ice::Variable *handle{}; |
| 4875 | Ice::Variable *promise{}; |
| 4876 | }; |
| 4877 | |
| 4878 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4879 | { |
| 4880 | // This doubles up as our coroutine handle |
| 4881 | auto coroData = coro::createCoroutineData(); |
| 4882 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4883 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4884 | if(coroData->useInternalScheduler) |
| 4885 | { |
| 4886 | ::getOrCreateScheduler().bind(); |
| 4887 | } |
| 4888 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4889 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4890 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4891 | // any fiber switch occurs. |
| 4892 | coro::setHandleParam(coroData); |
| 4893 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4894 | ASSERT(!coroData->routineFiber); |
| 4895 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4896 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4897 | beginFunc(); |
| 4898 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4899 | ASSERT(coroData->inRoutine); |
| 4900 | coroData->done = true; // coroutine is done. |
| 4901 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4902 | coroData->inRoutine = false; |
| 4903 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4904 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4905 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4906 | ASSERT(!coroData->mainFiber); |
| 4907 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4908 | |
| 4909 | // block until the first yield or coroutine end |
| 4910 | ASSERT(!coroData->inRoutine); |
| 4911 | coroData->inRoutine = true; |
| 4912 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4913 | while(coroData->inRoutine) |
| 4914 | { |
| 4915 | coroData->mainFiber->wait(); |
| 4916 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4917 | |
| 4918 | return coroData; |
| 4919 | } |
| 4920 | |
| 4921 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4922 | { |
| 4923 | // Start by creating a regular function |
| 4924 | createFunction(yieldType, params); |
| 4925 | |
| 4926 | // Save in case yield() is called |
| 4927 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4928 | ::coroYieldType = yieldType; |
| 4929 | } |
| 4930 | |
| 4931 | void Nucleus::yield(Value *val) |
| 4932 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4933 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4934 | Variable::materializeAll(); |
| 4935 | |
| 4936 | // On first yield, we start generating coroutine functions |
| 4937 | if(!::coroGen) |
| 4938 | { |
| 4939 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4940 | ::coroGen->generateCoroutineBegin(); |
| 4941 | } |
| 4942 | |
| 4943 | ASSERT(::coroGen); |
| 4944 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4945 | } |
| 4946 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4947 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4948 | { |
| 4949 | return false; |
| 4950 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4951 | |
| 4952 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4953 | { |
| 4954 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4955 | |
Sean Risser | 705231f | 2021-08-19 18:17:24 -0400 | [diff] [blame] | 4956 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit *cfgEdit /* = nullptr */) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4957 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4958 | if(::coroGen) |
| 4959 | { |
| 4960 | // Finish generating coroutine functions |
| 4961 | { |
| 4962 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4963 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4964 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4965 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4966 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4967 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4968 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4969 | // At this point, we no longer need the CoroutineGenerator. |
| 4970 | ::coroGen.reset(); |
| 4971 | ::coroYieldType = nullptr; |
| 4972 | |
| 4973 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4974 | { name, "await", "destroy" }, |
| 4975 | cfgEdit); |
| 4976 | |
| 4977 | return routine; |
| 4978 | } |
| 4979 | else |
| 4980 | { |
| 4981 | { |
| 4982 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4983 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | ::coroYieldType = nullptr; |
| 4987 | |
| 4988 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 4989 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 4990 | |
| 4991 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 4992 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 4993 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 4994 | return routine; |
| 4995 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4996 | } |
| 4997 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4998 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4999 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5000 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 5001 | |
| 5002 | if(isCoroutine) |
| 5003 | { |
| 5004 | return rr::invokeCoroutineBegin(func); |
| 5005 | } |
| 5006 | else |
| 5007 | { |
| 5008 | // For regular routines, just invoke the begin func directly |
| 5009 | return func(); |
| 5010 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5011 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5012 | |
| 5013 | } // namespace rr |