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" |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 17 | #include "Print.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 18 | #include "Reactor.hpp" |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 19 | #include "ReactorDebugInfo.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 20 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 21 | #include "ExecutableMemory.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 22 | #include "Optimizer.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 23 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 24 | #include "src/IceCfg.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 25 | #include "src/IceCfgNode.h" |
| 26 | #include "src/IceELFObjectWriter.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 27 | #include "src/IceELFStreamer.h" |
| 28 | #include "src/IceGlobalContext.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 29 | #include "src/IceGlobalInits.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 30 | #include "src/IceTypes.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 31 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Compiler.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 33 | #include "llvm/Support/FileSystem.h" |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 34 | #include "llvm/Support/ManagedStatic.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 35 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 36 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 37 | #include "marl/event.h" |
| 38 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 39 | #if __has_feature(memory_sanitizer) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 40 | # include <sanitizer/msan_interface.h> |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 41 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 42 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 43 | #if defined(_WIN32) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 44 | # ifndef WIN32_LEAN_AND_MEAN |
| 45 | # define WIN32_LEAN_AND_MEAN |
| 46 | # endif // !WIN32_LEAN_AND_MEAN |
| 47 | # ifndef NOMINMAX |
| 48 | # define NOMINMAX |
| 49 | # endif // !NOMINMAX |
| 50 | # include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 51 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 52 | |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 53 | #include <array> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 54 | #include <iostream> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 55 | #include <limits> |
| 56 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 57 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 58 | // Subzero utility functions |
| 59 | // 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] | 60 | namespace { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 61 | namespace sz { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 62 | |
| 63 | Ice::Cfg *createFunction(Ice::GlobalContext *context, Ice::Type returnType, const std::vector<Ice::Type> ¶mTypes) |
| 64 | { |
| 65 | uint32_t sequenceNumber = 0; |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame] | 66 | auto *function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 67 | |
| 68 | function->setStackSizeLimit(512 * 1024); // 512 KiB |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 69 | |
| 70 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 71 | |
| 72 | for(auto type : paramTypes) |
| 73 | { |
| 74 | Ice::Variable *arg = function->makeVariable(type); |
| 75 | function->addArg(arg); |
| 76 | } |
| 77 | |
| 78 | Ice::CfgNode *node = function->makeNode(); |
| 79 | function->setEntryNode(node); |
| 80 | |
| 81 | return function; |
| 82 | } |
| 83 | |
| 84 | Ice::Type getPointerType(Ice::Type elementType) |
| 85 | { |
| 86 | if(sizeof(void *) == 8) |
| 87 | { |
| 88 | return Ice::IceType_i64; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | return Ice::IceType_i32; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 97 | { |
| 98 | int typeSize = Ice::typeWidthInBytes(type); |
| 99 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 100 | |
| 101 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 102 | auto address = function->makeVariable(getPointerType(type)); |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 103 | 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] | 104 | function->getEntryNode()->getInsts().push_front(alloca); |
| 105 | |
| 106 | return address; |
| 107 | } |
| 108 | |
| 109 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 110 | { |
| 111 | if(sizeof(void *) == 8) |
| 112 | { |
| 113 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 118 | } |
| 119 | } |
| 120 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 121 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 122 | 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] | 123 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 124 | // Wrapper for calls on C functions with Ice types |
| 125 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 126 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 127 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 128 | |
| 129 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 130 | // then truncate result to bool. |
| 131 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 132 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 133 | if(returningBool) |
| 134 | { |
| 135 | ret = function->makeVariable(Ice::IceType_i32); |
| 136 | } |
| 137 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 138 | { |
| 139 | ret = function->makeVariable(retTy); |
| 140 | } |
| 141 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 142 | 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] | 143 | for(auto arg : iceArgs) |
| 144 | { |
| 145 | call->addArg(arg); |
| 146 | } |
| 147 | |
| 148 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 149 | |
| 150 | if(returningBool) |
| 151 | { |
| 152 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 153 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 154 | } |
| 155 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 156 | return ret; |
| 157 | } |
| 158 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 159 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 160 | { |
| 161 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 162 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 163 | } |
| 164 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 165 | // Wrapper for calls on C functions with Ice types |
| 166 | template<typename Return, typename... CArgs, typename... RArgs> |
Nicolas Capens | 629bf95 | 2022-01-18 15:08:14 -0500 | [diff] [blame] | 167 | 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] | 168 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 169 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 170 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 171 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 172 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 173 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 176 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 177 | { |
| 178 | Ice::Variable *to = function->makeVariable(toType); |
| 179 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 180 | basicBlock->appendInst(cast); |
| 181 | return to; |
| 182 | } |
| 183 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 184 | 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] | 185 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 186 | Ice::Variable *result = function->makeVariable(type); |
| 187 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 188 | basicBlock->appendInst(load); |
| 189 | |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 194 | } // namespace |
| 195 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 196 | namespace rr { |
| 197 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 198 | class CoroutineGenerator; |
| 199 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 200 | |
| 201 | namespace { |
| 202 | |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 203 | // Used to automatically invoke llvm_shutdown() when driver is unloaded |
| 204 | llvm::llvm_shutdown_obj llvmShutdownObj; |
| 205 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 206 | // Default configuration settings. Must be accessed under mutex lock. |
| 207 | std::mutex defaultConfigLock; |
| 208 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 209 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 210 | // This uses a static in a function to avoid the cost of a global static |
| 211 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 212 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 213 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 214 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 217 | Ice::GlobalContext *context = nullptr; |
| 218 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 219 | Ice::CfgNode *entryBlock = nullptr; |
| 220 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 221 | Ice::CfgNode *basicBlock = nullptr; |
| 222 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 223 | rr::ELFMemoryStreamer *routine = nullptr; |
| 224 | |
| 225 | std::mutex codegenMutex; |
| 226 | |
| 227 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 228 | Ice::Fdstream *out = nullptr; |
| 229 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 230 | // Coroutine globals |
| 231 | rr::Type *coroYieldType = nullptr; |
| 232 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 233 | marl::Scheduler &getOrCreateScheduler() |
| 234 | { |
| 235 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 236 | marl::Scheduler::Config cfg; |
| 237 | cfg.setWorkerThreadCount(8); |
| 238 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 239 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 240 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 241 | return *scheduler; |
| 242 | } |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 243 | |
| 244 | rr::Nucleus::OptimizerCallback *optimizerCallback = nullptr; |
| 245 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 246 | } // Anonymous namespace |
| 247 | |
| 248 | namespace { |
| 249 | |
| 250 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 251 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 252 | #endif |
| 253 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 254 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 255 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 256 | #endif |
| 257 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 258 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 259 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 260 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 261 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 262 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
| 263 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 264 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 265 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
| 266 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 267 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 268 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 269 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 272 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 273 | { |
| 274 | switch(memoryOrder) |
| 275 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 276 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 277 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 278 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 279 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 280 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 281 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 282 | } |
| 283 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 284 | } |
| 285 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 286 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 287 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 288 | public: |
| 289 | const static bool ARM; |
| 290 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 291 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 292 | private: |
| 293 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 294 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 295 | #if defined(__i386__) || defined(__x86_64__) |
| 296 | # if defined(_WIN32) |
| 297 | __cpuid(registers, info); |
| 298 | # else |
| 299 | __asm volatile("cpuid" |
| 300 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 301 | : "a"(info)); |
| 302 | # endif |
| 303 | #else |
| 304 | registers[0] = 0; |
| 305 | registers[1] = 0; |
| 306 | registers[2] = 0; |
| 307 | registers[3] = 0; |
| 308 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Sean Risser | 46a649d | 2021-08-30 15:44:33 -0400 | [diff] [blame] | 311 | constexpr static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 312 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 313 | #if defined(__arm__) || defined(__aarch64__) |
| 314 | return true; |
| 315 | #elif defined(__i386__) || defined(__x86_64__) |
| 316 | return false; |
| 317 | #elif defined(__mips__) |
| 318 | return false; |
| 319 | #else |
| 320 | # error "Unknown architecture" |
| 321 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 322 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 323 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 324 | static bool detectSSE4_1() |
| 325 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 326 | #if defined(__i386__) || defined(__x86_64__) |
| 327 | int registers[4]; |
| 328 | cpuid(registers, 1); |
| 329 | return (registers[2] & 0x00080000) != 0; |
| 330 | #else |
| 331 | return false; |
| 332 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 333 | } |
| 334 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 335 | |
Sean Risser | 46a649d | 2021-08-30 15:44:33 -0400 | [diff] [blame] | 336 | constexpr bool CPUID::ARM = CPUID::detectARM(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 337 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
Sean Risser | 46a649d | 2021-08-30 15:44:33 -0400 | [diff] [blame] | 338 | constexpr bool emulateIntrinsics = false; |
| 339 | constexpr bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 340 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 341 | constexpr bool subzeroDumpEnabled = false; |
| 342 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 343 | |
| 344 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 345 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 346 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 347 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 348 | |
| 349 | } // anonymous namespace |
| 350 | |
| 351 | namespace rr { |
| 352 | |
Nicolas Capens | 70505b4 | 2022-01-31 22:29:48 -0500 | [diff] [blame] | 353 | std::string Caps::backendName() |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 354 | { |
| 355 | return "Subzero"; |
| 356 | } |
| 357 | |
Nicolas Capens | 70505b4 | 2022-01-31 22:29:48 -0500 | [diff] [blame] | 358 | bool Caps::coroutinesSupported() |
| 359 | { |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | bool Caps::fmaIsFast() |
| 364 | { |
| 365 | // TODO(b/214591655): Subzero currently never emits FMA instructions. std::fma() is called instead. |
| 366 | return false; |
| 367 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 368 | |
| 369 | enum EmulatedType |
| 370 | { |
| 371 | EmulatedShift = 16, |
| 372 | EmulatedV2 = 2 << EmulatedShift, |
| 373 | EmulatedV4 = 4 << EmulatedShift, |
| 374 | EmulatedV8 = 8 << EmulatedShift, |
| 375 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 376 | |
| 377 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 378 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 379 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 380 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 381 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 382 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 383 | }; |
| 384 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 385 | class Value : public Ice::Operand |
| 386 | {}; |
| 387 | class SwitchCases : public Ice::InstSwitch |
| 388 | {}; |
| 389 | class BasicBlock : public Ice::CfgNode |
| 390 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 391 | |
| 392 | Ice::Type T(Type *t) |
| 393 | { |
| 394 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 395 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 398 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 399 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 400 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | Type *T(EmulatedType t) |
| 404 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 405 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 408 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 409 | { |
| 410 | std::vector<Ice::Type> result; |
| 411 | result.reserve(types.size()); |
| 412 | for(auto &t : types) |
| 413 | { |
| 414 | result.push_back(T(t)); |
| 415 | } |
| 416 | return result; |
| 417 | } |
| 418 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 419 | Value *V(Ice::Operand *v) |
| 420 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 421 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 422 | } |
| 423 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 424 | Ice::Operand *V(Value *v) |
| 425 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 426 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 429 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 430 | { |
| 431 | std::vector<Ice::Operand *> result; |
| 432 | result.reserve(values.size()); |
| 433 | for(auto &v : values) |
| 434 | { |
| 435 | result.push_back(V(v)); |
| 436 | } |
| 437 | return result; |
| 438 | } |
| 439 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 440 | BasicBlock *B(Ice::CfgNode *b) |
| 441 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 442 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static size_t typeSize(Type *type) |
| 446 | { |
| 447 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 448 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 449 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 450 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 451 | case Type_v2i32: return 8; |
| 452 | case Type_v4i16: return 8; |
| 453 | case Type_v2i16: return 4; |
| 454 | case Type_v8i8: return 8; |
| 455 | case Type_v4i8: return 4; |
| 456 | case Type_v2f32: return 8; |
| 457 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | return Ice::typeWidthInBytes(T(type)); |
| 462 | } |
| 463 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 464 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 465 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 466 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 467 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 468 | { |
| 469 | Nucleus::createRetVoid(); |
| 470 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 471 | |
| 472 | // Connect the entry block to the top of the initial basic block |
| 473 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 474 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 477 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 478 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 479 | |
| 480 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 481 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 482 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 486 | { |
| 487 | return §ionHeader(elfHeader)[index]; |
| 488 | } |
| 489 | |
| 490 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 491 | { |
| 492 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 493 | |
| 494 | uint32_t index = relocation.getSymbol(); |
| 495 | int table = relocationTable.sh_link; |
| 496 | void *symbolValue = nullptr; |
| 497 | |
| 498 | if(index != SHN_UNDEF) |
| 499 | { |
| 500 | if(table == SHN_UNDEF) return nullptr; |
| 501 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 502 | |
| 503 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 504 | if(index >= symtab_entries) |
| 505 | { |
| 506 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 507 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 510 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 511 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 512 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 513 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 514 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 515 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 516 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 517 | 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] | 518 | } |
| 519 | else |
| 520 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 521 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 522 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 523 | } |
| 524 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 525 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 526 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | |
| 528 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 529 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 530 | switch(relocation.getType()) |
| 531 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 532 | case R_ARM_NONE: |
| 533 | // No relocation |
| 534 | break; |
| 535 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 536 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 537 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 538 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 539 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 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 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 543 | { |
| 544 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 545 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 546 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 547 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 548 | default: |
| 549 | ASSERT(false && "Unsupported relocation type"); |
| 550 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 551 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 552 | } |
| 553 | else |
| 554 | { |
| 555 | switch(relocation.getType()) |
| 556 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 557 | case R_386_NONE: |
| 558 | // No relocation |
| 559 | break; |
| 560 | case R_386_32: |
| 561 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 562 | break; |
| 563 | case R_386_PC32: |
| 564 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 565 | break; |
| 566 | default: |
| 567 | ASSERT(false && "Unsupported relocation type"); |
| 568 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 569 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 572 | return symbolValue; |
| 573 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 574 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 575 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 576 | { |
| 577 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 578 | |
| 579 | uint32_t index = relocation.getSymbol(); |
| 580 | int table = relocationTable.sh_link; |
| 581 | void *symbolValue = nullptr; |
| 582 | |
| 583 | if(index != SHN_UNDEF) |
| 584 | { |
| 585 | if(table == SHN_UNDEF) return nullptr; |
| 586 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 587 | |
| 588 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 589 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 590 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 591 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 592 | return nullptr; |
| 593 | } |
| 594 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 595 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 596 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 597 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 598 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 599 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 600 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 601 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 602 | 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] | 603 | } |
| 604 | else |
| 605 | { |
| 606 | return nullptr; |
| 607 | } |
| 608 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 609 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 610 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 611 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 612 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 613 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 614 | switch(relocation.getType()) |
| 615 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 616 | case R_X86_64_NONE: |
| 617 | // No relocation |
| 618 | break; |
| 619 | case R_X86_64_64: |
| 620 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 621 | break; |
| 622 | case R_X86_64_PC32: |
| 623 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 624 | break; |
| 625 | case R_X86_64_32S: |
| 626 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 627 | break; |
| 628 | default: |
| 629 | ASSERT(false && "Unsupported relocation type"); |
| 630 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | return symbolValue; |
| 634 | } |
| 635 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 636 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 637 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 638 | const void *entry; |
| 639 | size_t codeSize = 0; |
| 640 | }; |
| 641 | |
| 642 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 643 | { |
| 644 | ASSERT(functionNames.size() > 0); |
| 645 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 646 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 647 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 648 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 649 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 650 | if(!elfHeader->checkMagic()) |
| 651 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 652 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 656 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 657 | #if defined(__i386__) |
| 658 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 659 | #elif defined(__x86_64__) |
| 660 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 661 | #elif defined(__arm__) |
| 662 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 663 | #elif defined(__aarch64__) |
| 664 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 665 | #elif defined(__mips__) |
| 666 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 667 | #else |
| 668 | # error "Unsupported platform" |
| 669 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 670 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 671 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 672 | |
| 673 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 674 | { |
| 675 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 676 | { |
| 677 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 678 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 679 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 680 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 681 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 682 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 683 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 684 | { |
| 685 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 686 | { |
| 687 | return j; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 692 | return static_cast<size_t>(-1); |
| 693 | }; |
| 694 | |
| 695 | size_t index = findSectionNameEntryIndex(); |
| 696 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 697 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 698 | } |
| 699 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 700 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 701 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 702 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 703 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 704 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 705 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 706 | 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] | 707 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 708 | } |
| 709 | } |
| 710 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 711 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 712 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 713 | |
| 714 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 715 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 716 | 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] | 717 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 722 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | template<typename T> |
| 726 | struct ExecutableAllocator |
| 727 | { |
| 728 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 729 | template<class U> |
| 730 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 731 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 732 | |
| 733 | using value_type = T; |
| 734 | using size_type = std::size_t; |
| 735 | |
| 736 | T *allocate(size_type n) |
| 737 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 738 | return (T *)allocateMemoryPages( |
| 739 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | void deallocate(T *p, size_type n) |
| 743 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 744 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 745 | } |
| 746 | }; |
| 747 | |
| 748 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 749 | { |
| 750 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 751 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 752 | |
| 753 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 754 | ELFMemoryStreamer() |
| 755 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 756 | { |
| 757 | position = 0; |
| 758 | buffer.reserve(0x1000); |
| 759 | } |
| 760 | |
| 761 | ~ELFMemoryStreamer() override |
| 762 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void write8(uint8_t Value) override |
| 766 | { |
| 767 | if(position == (uint64_t)buffer.size()) |
| 768 | { |
| 769 | buffer.push_back(Value); |
| 770 | position++; |
| 771 | } |
| 772 | else if(position < (uint64_t)buffer.size()) |
| 773 | { |
| 774 | buffer[position] = Value; |
| 775 | position++; |
| 776 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 777 | else |
| 778 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void writeBytes(llvm::StringRef Bytes) override |
| 782 | { |
| 783 | std::size_t oldSize = buffer.size(); |
| 784 | buffer.resize(oldSize + Bytes.size()); |
| 785 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 786 | position += Bytes.size(); |
| 787 | } |
| 788 | |
| 789 | uint64_t tell() const override { return position; } |
| 790 | |
| 791 | void seek(uint64_t Off) override { position = Off; } |
| 792 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 793 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 794 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 795 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 796 | |
| 797 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 798 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 799 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 800 | for(auto &entryPoint : entryPoints) |
| 801 | { |
| 802 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 803 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 804 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 805 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 806 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 809 | void finalize() |
| 810 | { |
| 811 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 812 | |
| 813 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 814 | } |
| 815 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 816 | void setEntry(int index, const void *func) |
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(func); |
| 819 | funcs[index] = func; |
| 820 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 821 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 822 | const void *getEntry(int index) const override |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 823 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 824 | ASSERT(funcs[index]); |
| 825 | return funcs[index]; |
| 826 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 827 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 828 | 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] | 829 | { |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 830 | // Check if we already have a suitable constant. |
| 831 | for(const auto &c : constantsPool) |
| 832 | { |
| 833 | void *ptr = c.data.get(); |
| 834 | size_t space = c.space; |
| 835 | |
| 836 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 837 | |
| 838 | if(space < size) |
| 839 | { |
| 840 | continue; |
| 841 | } |
| 842 | |
| 843 | if(memcmp(data, alignedPtr, size) == 0) |
| 844 | { |
| 845 | return alignedPtr; |
| 846 | } |
| 847 | } |
| 848 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 849 | // TODO(b/148086935): Replace with a buffer allocator. |
| 850 | size_t space = size + alignment; |
| 851 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 852 | void *ptr = buf.get(); |
| 853 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 854 | ASSERT(alignedPtr); |
| 855 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 856 | constantsPool.emplace_back(std::move(buf), space); |
| 857 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 858 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 859 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 860 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 861 | private: |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 862 | struct Constant |
| 863 | { |
| 864 | Constant(std::unique_ptr<uint8_t[]> data, size_t space) |
| 865 | : data(std::move(data)) |
| 866 | , space(space) |
| 867 | {} |
| 868 | |
| 869 | std::unique_ptr<uint8_t[]> data; |
| 870 | size_t space; |
| 871 | }; |
| 872 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 873 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 874 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 875 | std::size_t position; |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 876 | std::vector<Constant> constantsPool; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 877 | }; |
| 878 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 879 | #ifdef ENABLE_RR_PRINT |
| 880 | void VPrintf(const std::vector<Value *> &vals) |
| 881 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 882 | 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] | 883 | } |
| 884 | #endif // ENABLE_RR_PRINT |
| 885 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 886 | Nucleus::Nucleus() |
| 887 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 888 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 889 | |
| 890 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 891 | Ice::ClFlags::getParsedClFlags(Flags); |
| 892 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 893 | #if defined(__arm__) |
| 894 | Flags.setTargetArch(Ice::Target_ARM32); |
| 895 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 896 | #elif defined(__mips__) |
| 897 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 898 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 899 | #else // x86 |
| 900 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 901 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 902 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 903 | Flags.setOutFileType(Ice::FT_Elf); |
| 904 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 905 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 906 | Flags.setDisableHybridAssembly(true); |
| 907 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 908 | // Emit functions into separate sections in the ELF so we can find them by name |
| 909 | Flags.setFunctionSections(true); |
| 910 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 911 | static llvm::raw_os_ostream cout(std::cout); |
| 912 | static llvm::raw_os_ostream cerr(std::cerr); |
| 913 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 914 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 915 | { |
| 916 | // Decorate text asm with liveness info |
| 917 | Flags.setDecorateAsm(true); |
| 918 | } |
| 919 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 920 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 921 | { |
| 922 | std::error_code errorCode; |
| 923 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 924 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 925 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 926 | } |
| 927 | else |
| 928 | { |
| 929 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 930 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 931 | ::routine = elfMemory; |
| 932 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 933 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 934 | #if !__has_feature(memory_sanitizer) |
| 935 | // thread_local variables in shared libraries are initialized at load-time, |
| 936 | // 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] | 937 | // instrumented, leading to false-positive uninitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 938 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 939 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 940 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | Nucleus::~Nucleus() |
| 944 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 945 | delete Variable::unmaterializedVariables; |
| 946 | Variable::unmaterializedVariables = nullptr; |
| 947 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 948 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 949 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 950 | |
| 951 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 952 | ::allocator = nullptr; |
| 953 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 954 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 955 | ::function = nullptr; |
| 956 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 957 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 958 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 959 | |
| 960 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 961 | ::elfFile = nullptr; |
| 962 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 963 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 964 | ::out = nullptr; |
| 965 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 966 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 967 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 968 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 969 | |
| 970 | ::codegenMutex.unlock(); |
| 971 | } |
| 972 | |
| 973 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 974 | { |
| 975 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 976 | ::defaultConfig() = cfg; |
| 977 | } |
| 978 | |
| 979 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 980 | { |
| 981 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 982 | auto &config = ::defaultConfig(); |
| 983 | config = cfgEdit.apply(config); |
| 984 | } |
| 985 | |
| 986 | Config Nucleus::getDefaultConfig() |
| 987 | { |
| 988 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 989 | return ::defaultConfig(); |
| 990 | } |
| 991 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 992 | // This function lowers and produces executable binary code in memory for the input functions, |
| 993 | // and returns a Routine with the entry points to these functions. |
| 994 | template<size_t Count> |
Sean Risser | 705231f | 2021-08-19 18:17:24 -0400 | [diff] [blame] | 995 | 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] | 996 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 997 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 998 | // and GlobalContext::emitItems. |
| 999 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1000 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1001 | { |
| 1002 | // 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] | 1003 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | ::context->emitFileHeader(); |
| 1007 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1008 | // Translate |
| 1009 | |
| 1010 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1011 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1012 | Ice::Cfg *currFunc = functions[i]; |
| 1013 | |
| 1014 | // Install function allocator in TLS for Cfg-specific container allocators |
| 1015 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 1016 | |
| 1017 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 1018 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 1019 | if(::optimizerCallback) |
| 1020 | { |
| 1021 | Nucleus::OptimizerReport report; |
| 1022 | rr::optimize(currFunc, &report); |
| 1023 | ::optimizerCallback(&report); |
| 1024 | ::optimizerCallback = nullptr; |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | rr::optimize(currFunc); |
| 1029 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1030 | |
| 1031 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1032 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1033 | |
| 1034 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1035 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1036 | |
| 1037 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1038 | |
| 1039 | if(subzeroEmitTextAsm) |
| 1040 | { |
| 1041 | currFunc->emit(); |
| 1042 | } |
| 1043 | |
| 1044 | currFunc->emitIAS(); |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame] | 1045 | |
| 1046 | if(currFunc->hasError()) |
| 1047 | { |
| 1048 | return nullptr; |
| 1049 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1050 | } |
| 1051 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1052 | // Emit items |
| 1053 | |
| 1054 | ::context->lowerGlobals(""); |
| 1055 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1056 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1057 | |
| 1058 | for(size_t i = 0; i < Count; ++i) |
| 1059 | { |
| 1060 | Ice::Cfg *currFunc = functions[i]; |
| 1061 | |
| 1062 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1063 | auto globals = currFunc->getGlobalInits(); |
| 1064 | if(globals && !globals->empty()) |
| 1065 | { |
| 1066 | ::context->getGlobals()->merge(globals.get()); |
| 1067 | } |
| 1068 | |
| 1069 | auto assembler = currFunc->releaseAssembler(); |
| 1070 | assembler->alignFunction(); |
| 1071 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1072 | } |
| 1073 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1074 | ::context->lowerGlobals("last"); |
| 1075 | ::context->lowerConstants(); |
| 1076 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1077 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1078 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1079 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1080 | objectWriter->writeNonUserSections(); |
| 1081 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1082 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1083 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1084 | ASSERT(entryPoints.size() == Count); |
| 1085 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1086 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1087 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1091 | |
| 1092 | Routine *handoffRoutine = ::routine; |
| 1093 | ::routine = nullptr; |
| 1094 | |
| 1095 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1096 | } |
| 1097 | |
Sean Risser | 705231f | 2021-08-19 18:17:24 -0400 | [diff] [blame] | 1098 | 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] | 1099 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1100 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1101 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1102 | } |
| 1103 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1104 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1105 | { |
| 1106 | Ice::Type type = T(t); |
| 1107 | int typeSize = Ice::typeWidthInBytes(type); |
| 1108 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1109 | |
| 1110 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1111 | auto address = ::function->makeVariable(T(getPointerType(t))); |
Nicolas Capens | 0cfc043 | 2021-02-05 15:18:42 -0500 | [diff] [blame] | 1112 | 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] | 1113 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1114 | |
| 1115 | return V(address); |
| 1116 | } |
| 1117 | |
| 1118 | BasicBlock *Nucleus::createBasicBlock() |
| 1119 | { |
| 1120 | return B(::function->makeNode()); |
| 1121 | } |
| 1122 | |
| 1123 | BasicBlock *Nucleus::getInsertBlock() |
| 1124 | { |
| 1125 | return B(::basicBlock); |
| 1126 | } |
| 1127 | |
| 1128 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1129 | { |
Nicolas Capens | 7c296ec | 2021-02-18 14:10:26 -0500 | [diff] [blame] | 1130 | // 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] | 1131 | |
| 1132 | ::basicBlock = basicBlock; |
| 1133 | } |
| 1134 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1135 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1136 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1137 | ASSERT(::function == nullptr); |
| 1138 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1139 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1140 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1141 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1142 | |
| 1143 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1144 | |
| 1145 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1146 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1147 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1148 | // 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] | 1149 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1150 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1151 | ::entryBlock = ::function->getEntryNode(); |
| 1152 | ::basicBlock = ::function->makeNode(); |
| 1153 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | Value *Nucleus::getArgument(unsigned int index) |
| 1157 | { |
| 1158 | return V(::function->getArgs()[index]); |
| 1159 | } |
| 1160 | |
| 1161 | void Nucleus::createRetVoid() |
| 1162 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1163 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1164 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1165 | // Code generated after this point is unreachable, so any variables |
| 1166 | // being read can safely return an undefined value. We have to avoid |
| 1167 | // materializing variables after the terminator ret instruction. |
| 1168 | Variable::killUnmaterialized(); |
| 1169 | |
| 1170 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1171 | ::basicBlock->appendInst(ret); |
| 1172 | } |
| 1173 | |
| 1174 | void Nucleus::createRet(Value *v) |
| 1175 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1176 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1177 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1178 | // Code generated after this point is unreachable, so any variables |
| 1179 | // being read can safely return an undefined value. We have to avoid |
| 1180 | // materializing variables after the terminator ret instruction. |
| 1181 | Variable::killUnmaterialized(); |
| 1182 | |
| 1183 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1184 | ::basicBlock->appendInst(ret); |
| 1185 | } |
| 1186 | |
| 1187 | void Nucleus::createBr(BasicBlock *dest) |
| 1188 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1189 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1190 | Variable::materializeAll(); |
| 1191 | |
| 1192 | auto br = Ice::InstBr::create(::function, dest); |
| 1193 | ::basicBlock->appendInst(br); |
| 1194 | } |
| 1195 | |
| 1196 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1197 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1198 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1199 | Variable::materializeAll(); |
| 1200 | |
| 1201 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1202 | ::basicBlock->appendInst(br); |
| 1203 | } |
| 1204 | |
| 1205 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1206 | { |
| 1207 | switch(op) |
| 1208 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1209 | case Ice::InstArithmetic::Add: |
| 1210 | case Ice::InstArithmetic::Fadd: |
| 1211 | case Ice::InstArithmetic::Mul: |
| 1212 | case Ice::InstArithmetic::Fmul: |
| 1213 | case Ice::InstArithmetic::And: |
| 1214 | case Ice::InstArithmetic::Or: |
| 1215 | case Ice::InstArithmetic::Xor: |
| 1216 | return true; |
| 1217 | default: |
| 1218 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1223 | { |
| 1224 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1225 | |
| 1226 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1227 | |
| 1228 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1229 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1230 | ::basicBlock->appendInst(arithmetic); |
| 1231 | |
| 1232 | return V(result); |
| 1233 | } |
| 1234 | |
| 1235 | Value *Nucleus::createAdd(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::Add, lhs, rhs); |
| 1239 | } |
| 1240 | |
| 1241 | Value *Nucleus::createSub(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::Sub, lhs, rhs); |
| 1245 | } |
| 1246 | |
| 1247 | Value *Nucleus::createMul(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::Mul, lhs, rhs); |
| 1251 | } |
| 1252 | |
| 1253 | Value *Nucleus::createUDiv(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::Udiv, lhs, rhs); |
| 1257 | } |
| 1258 | |
| 1259 | Value *Nucleus::createSDiv(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::Sdiv, lhs, rhs); |
| 1263 | } |
| 1264 | |
| 1265 | Value *Nucleus::createFAdd(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::Fadd, lhs, rhs); |
| 1269 | } |
| 1270 | |
| 1271 | Value *Nucleus::createFSub(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::Fsub, lhs, rhs); |
| 1275 | } |
| 1276 | |
| 1277 | Value *Nucleus::createFMul(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::Fmul, lhs, rhs); |
| 1281 | } |
| 1282 | |
| 1283 | Value *Nucleus::createFDiv(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::Fdiv, lhs, rhs); |
| 1287 | } |
| 1288 | |
| 1289 | Value *Nucleus::createURem(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::Urem, lhs, rhs); |
| 1293 | } |
| 1294 | |
| 1295 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1296 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1297 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1298 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
| 1299 | } |
| 1300 | |
| 1301 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1302 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1303 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1304 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1305 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1306 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1307 | return nullptr; |
| 1308 | } |
| 1309 | |
| 1310 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1311 | { |
| 1312 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | Value *Nucleus::createShl(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::Shl, lhs, rhs); |
| 1319 | } |
| 1320 | |
| 1321 | Value *Nucleus::createLShr(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::Lshr, lhs, rhs); |
| 1325 | } |
| 1326 | |
| 1327 | Value *Nucleus::createAShr(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::Ashr, lhs, rhs); |
| 1331 | } |
| 1332 | |
| 1333 | Value *Nucleus::createAnd(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::And, lhs, rhs); |
| 1337 | } |
| 1338 | |
| 1339 | Value *Nucleus::createOr(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::Or, lhs, rhs); |
| 1343 | } |
| 1344 | |
| 1345 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 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 createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1349 | } |
| 1350 | |
| 1351 | Value *Nucleus::createNeg(Value *v) |
| 1352 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1353 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1354 | return createSub(createNullValue(T(v->getType())), v); |
| 1355 | } |
| 1356 | |
| 1357 | Value *Nucleus::createFNeg(Value *v) |
| 1358 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1359 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1360 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1361 | 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] | 1362 | |
| 1363 | return createFSub(negativeZero, v); |
| 1364 | } |
| 1365 | |
| 1366 | Value *Nucleus::createNot(Value *v) |
| 1367 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1368 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1369 | if(Ice::isScalarIntegerType(v->getType())) |
| 1370 | { |
| 1371 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1372 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1373 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1374 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1375 | 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] | 1376 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1380 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1381 | { |
| 1382 | #if defined(__i386__) || defined(__x86_64__) |
| 1383 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1384 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1385 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1386 | #else |
| 1387 | if(atomic) |
| 1388 | { |
| 1389 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1390 | } |
| 1391 | if(memoryOrder != std::memory_order_relaxed) |
| 1392 | { |
| 1393 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1394 | } |
| 1395 | #endif |
| 1396 | |
| 1397 | // Vulkan doesn't allow sequential memory order |
| 1398 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1399 | } |
| 1400 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1401 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1402 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1403 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1404 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1405 | |
| 1406 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1407 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1408 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1409 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1410 | { |
| 1411 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1412 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1413 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1414 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1415 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1416 | Int x = *Pointer<Int>(pointer); |
| 1417 | |
| 1418 | Int4 vector; |
| 1419 | vector = Insert(vector, x, 0); |
| 1420 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1421 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1422 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1423 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1424 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1425 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1426 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1427 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1428 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1429 | Int x = *Pointer<Int>(pointer); |
| 1430 | Int y = *Pointer<Int>(pointer + 4); |
| 1431 | |
| 1432 | Int4 vector; |
| 1433 | vector = Insert(vector, x, 0); |
| 1434 | vector = Insert(vector, y, 1); |
| 1435 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1436 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1437 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1438 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1439 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1440 | else |
| 1441 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1442 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1443 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1444 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1445 | 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] | 1446 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame] | 1447 | auto load = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1448 | load->addArg(ptr); |
| 1449 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1450 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1451 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1452 | } |
| 1453 | else |
| 1454 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1455 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1456 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1457 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1458 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1459 | return V(result); |
| 1460 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1461 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1462 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1463 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1464 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1465 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1466 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1467 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1468 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1469 | if(align != 0) |
| 1470 | { |
| 1471 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1472 | call->addArg(ptr); |
| 1473 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1474 | ::basicBlock->appendInst(call); |
| 1475 | } |
| 1476 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1477 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1478 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1479 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1480 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1481 | { |
| 1482 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1483 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1484 | if(typeSize(type) == 4) |
| 1485 | { |
| 1486 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1487 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1488 | ::basicBlock->appendInst(bitcast); |
| 1489 | |
| 1490 | RValue<Int4> v(V(vector)); |
| 1491 | |
| 1492 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1493 | Int x = Extract(v, 0); |
| 1494 | *Pointer<Int>(pointer) = x; |
| 1495 | } |
| 1496 | else if(typeSize(type) == 8) |
| 1497 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1498 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1499 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1500 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1501 | ::basicBlock->appendInst(bitcast); |
| 1502 | |
| 1503 | RValue<Int4> v(V(vector)); |
| 1504 | |
| 1505 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1506 | Int x = Extract(v, 0); |
| 1507 | *Pointer<Int>(pointer) = x; |
| 1508 | Int y = Extract(v, 1); |
| 1509 | *Pointer<Int>(pointer + 4) = y; |
| 1510 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1511 | else |
| 1512 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1513 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1514 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1515 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1516 | 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] | 1517 | auto store = Ice::InstIntrinsic::create(::function, 3, nullptr, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1518 | store->addArg(value); |
| 1519 | store->addArg(ptr); |
| 1520 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1521 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1522 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1523 | } |
| 1524 | else |
| 1525 | { |
| 1526 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1527 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1528 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1529 | ::basicBlock->appendInst(store); |
| 1530 | } |
| 1531 | |
| 1532 | return value; |
| 1533 | } |
| 1534 | |
| 1535 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1536 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1537 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1538 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1539 | |
| 1540 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1541 | { |
| 1542 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1543 | |
| 1544 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1545 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1546 | return ptr; |
| 1547 | } |
| 1548 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1549 | return createAdd(ptr, createConstantInt(offset)); |
| 1550 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1551 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1552 | if(!Ice::isByteSizedType(T(type))) |
| 1553 | { |
| 1554 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1555 | } |
| 1556 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1557 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1558 | { |
| 1559 | if(unsignedIndex) |
| 1560 | { |
| 1561 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1562 | } |
| 1563 | else |
| 1564 | { |
| 1565 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | return createAdd(ptr, index); |
| 1570 | } |
| 1571 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1572 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1573 | { |
| 1574 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1575 | |
| 1576 | 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] | 1577 | auto inst = Ice::InstIntrinsic::create(::function, 0, result, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1578 | auto op = ::context->getConstantInt32(rmwOp); |
| 1579 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1580 | inst->addArg(op); |
| 1581 | inst->addArg(ptr); |
| 1582 | inst->addArg(value); |
| 1583 | inst->addArg(order); |
| 1584 | ::basicBlock->appendInst(inst); |
| 1585 | |
| 1586 | return V(result); |
| 1587 | } |
| 1588 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1589 | Value *Nucleus::createAtomicAdd(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::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | Value *Nucleus::createAtomicSub(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::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | Value *Nucleus::createAtomicAnd(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::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | Value *Nucleus::createAtomicOr(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::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | Value *Nucleus::createAtomicXor(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::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 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 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1626 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1627 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1628 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1629 | |
| 1630 | 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] | 1631 | auto inst = Ice::InstIntrinsic::create(::function, 0, result, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1632 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1633 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1634 | inst->addArg(ptr); |
| 1635 | inst->addArg(compare); |
| 1636 | inst->addArg(value); |
| 1637 | inst->addArg(orderEq); |
| 1638 | inst->addArg(orderNeq); |
| 1639 | ::basicBlock->appendInst(inst); |
| 1640 | |
| 1641 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1645 | { |
| 1646 | if(v->getType() == T(destType)) |
| 1647 | { |
| 1648 | return v; |
| 1649 | } |
| 1650 | |
| 1651 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1652 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1653 | ::basicBlock->appendInst(cast); |
| 1654 | |
| 1655 | return V(result); |
| 1656 | } |
| 1657 | |
| 1658 | Value *Nucleus::createTrunc(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::Trunc, v, destType); |
| 1662 | } |
| 1663 | |
| 1664 | Value *Nucleus::createZExt(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::Zext, v, destType); |
| 1668 | } |
| 1669 | |
| 1670 | Value *Nucleus::createSExt(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::Sext, v, destType); |
| 1674 | } |
| 1675 | |
| 1676 | Value *Nucleus::createFPToUI(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::Fptoui, v, destType); |
| 1680 | } |
| 1681 | |
| 1682 | Value *Nucleus::createFPToSI(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::Fptosi, v, destType); |
| 1686 | } |
| 1687 | |
| 1688 | Value *Nucleus::createSIToFP(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::Sitofp, v, destType); |
| 1692 | } |
| 1693 | |
| 1694 | Value *Nucleus::createFPTrunc(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::Fptrunc, v, destType); |
| 1698 | } |
| 1699 | |
| 1700 | Value *Nucleus::createFPExt(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 | return createCast(Ice::InstCast::Fpext, v, destType); |
| 1704 | } |
| 1705 | |
| 1706 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1707 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1708 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1709 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1710 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1711 | // emulate them by writing to the stack and reading back as the destination type. |
| 1712 | if(emulateMismatchedBitCast) |
| 1713 | { |
| 1714 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1715 | { |
| 1716 | Value *address = allocateStackVariable(destType); |
| 1717 | createStore(v, address, T(v->getType())); |
| 1718 | return createLoad(address, destType); |
| 1719 | } |
| 1720 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1721 | { |
| 1722 | Value *address = allocateStackVariable(T(v->getType())); |
| 1723 | createStore(v, address, T(v->getType())); |
| 1724 | return createLoad(address, destType); |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1729 | } |
| 1730 | |
| 1731 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1732 | { |
| 1733 | ASSERT(lhs->getType() == rhs->getType()); |
| 1734 | |
| 1735 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1736 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1737 | ::basicBlock->appendInst(cmp); |
| 1738 | |
| 1739 | return V(result); |
| 1740 | } |
| 1741 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1742 | Value *Nucleus::createICmpEQ(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::Eq, lhs, rhs); |
| 1746 | } |
| 1747 | |
| 1748 | Value *Nucleus::createICmpNE(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::Ne, lhs, rhs); |
| 1752 | } |
| 1753 | |
| 1754 | Value *Nucleus::createICmpUGT(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::Ugt, lhs, rhs); |
| 1758 | } |
| 1759 | |
| 1760 | Value *Nucleus::createICmpUGE(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::Uge, lhs, rhs); |
| 1764 | } |
| 1765 | |
| 1766 | Value *Nucleus::createICmpULT(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::Ult, lhs, rhs); |
| 1770 | } |
| 1771 | |
| 1772 | Value *Nucleus::createICmpULE(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::Ule, lhs, rhs); |
| 1776 | } |
| 1777 | |
| 1778 | Value *Nucleus::createICmpSGT(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::Sgt, lhs, rhs); |
| 1782 | } |
| 1783 | |
| 1784 | Value *Nucleus::createICmpSGE(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::Sge, lhs, rhs); |
| 1788 | } |
| 1789 | |
| 1790 | Value *Nucleus::createICmpSLT(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::Slt, lhs, rhs); |
| 1794 | } |
| 1795 | |
| 1796 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1797 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1798 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1799 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1800 | } |
| 1801 | |
| 1802 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1803 | { |
| 1804 | ASSERT(lhs->getType() == rhs->getType()); |
| 1805 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1806 | |
| 1807 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1808 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1809 | ::basicBlock->appendInst(cmp); |
| 1810 | |
| 1811 | return V(result); |
| 1812 | } |
| 1813 | |
| 1814 | Value *Nucleus::createFCmpOEQ(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::Oeq, lhs, rhs); |
| 1818 | } |
| 1819 | |
| 1820 | Value *Nucleus::createFCmpOGT(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::Ogt, lhs, rhs); |
| 1824 | } |
| 1825 | |
| 1826 | Value *Nucleus::createFCmpOGE(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::Oge, lhs, rhs); |
| 1830 | } |
| 1831 | |
| 1832 | Value *Nucleus::createFCmpOLT(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::Olt, lhs, rhs); |
| 1836 | } |
| 1837 | |
| 1838 | Value *Nucleus::createFCmpOLE(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::Ole, lhs, rhs); |
| 1842 | } |
| 1843 | |
| 1844 | Value *Nucleus::createFCmpONE(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::One, lhs, rhs); |
| 1848 | } |
| 1849 | |
| 1850 | Value *Nucleus::createFCmpORD(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::Ord, lhs, rhs); |
| 1854 | } |
| 1855 | |
| 1856 | Value *Nucleus::createFCmpUNO(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::Uno, lhs, rhs); |
| 1860 | } |
| 1861 | |
| 1862 | Value *Nucleus::createFCmpUEQ(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::Ueq, lhs, rhs); |
| 1866 | } |
| 1867 | |
| 1868 | Value *Nucleus::createFCmpUGT(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::Ugt, lhs, rhs); |
| 1872 | } |
| 1873 | |
| 1874 | Value *Nucleus::createFCmpUGE(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::Uge, lhs, rhs); |
| 1878 | } |
| 1879 | |
| 1880 | Value *Nucleus::createFCmpULT(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::Ult, lhs, rhs); |
| 1884 | } |
| 1885 | |
| 1886 | Value *Nucleus::createFCmpULE(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::Ule, lhs, rhs); |
| 1890 | } |
| 1891 | |
| 1892 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 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 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1896 | } |
| 1897 | |
| 1898 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1899 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1900 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1901 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1902 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1903 | ::basicBlock->appendInst(extract); |
| 1904 | |
| 1905 | return V(result); |
| 1906 | } |
| 1907 | |
| 1908 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1909 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1910 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1911 | auto result = ::function->makeVariable(vector->getType()); |
| 1912 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1913 | ::basicBlock->appendInst(insert); |
| 1914 | |
| 1915 | return V(result); |
| 1916 | } |
| 1917 | |
| 1918 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 1919 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1920 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1921 | ASSERT(V1->getType() == V2->getType()); |
| 1922 | |
| 1923 | int size = Ice::typeNumElements(V1->getType()); |
| 1924 | auto result = ::function->makeVariable(V1->getType()); |
| 1925 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1926 | |
| 1927 | for(int i = 0; i < size; i++) |
| 1928 | { |
| 1929 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1930 | } |
| 1931 | |
| 1932 | ::basicBlock->appendInst(shuffle); |
| 1933 | |
| 1934 | return V(result); |
| 1935 | } |
| 1936 | |
| 1937 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1938 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1939 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1940 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1941 | |
| 1942 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1943 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1944 | ::basicBlock->appendInst(select); |
| 1945 | |
| 1946 | return V(result); |
| 1947 | } |
| 1948 | |
| 1949 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1950 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1951 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1952 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1953 | ::basicBlock->appendInst(switchInst); |
| 1954 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1955 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 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 | switchCases->addBranch(label, label, branch); |
| 1962 | } |
| 1963 | |
| 1964 | void Nucleus::createUnreachable() |
| 1965 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1966 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1967 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1968 | ::basicBlock->appendInst(unreachable); |
| 1969 | } |
| 1970 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1971 | Type *Nucleus::getType(Value *value) |
| 1972 | { |
| 1973 | return T(V(value)->getType()); |
| 1974 | } |
| 1975 | |
| 1976 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1977 | { |
| 1978 | Ice::Type vecTy = T(vectorType); |
| 1979 | switch(vecTy) |
| 1980 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 1981 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1982 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1983 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1984 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1985 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1986 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1987 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1988 | default: |
| 1989 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 1990 | return {}; |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1994 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1995 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1996 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1997 | } |
| 1998 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1999 | static constexpr Ice::Type getNaturalIntType() |
| 2000 | { |
| 2001 | constexpr size_t intSize = sizeof(int); |
| 2002 | static_assert(intSize == 4 || intSize == 8, ""); |
| 2003 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 2004 | } |
| 2005 | |
| 2006 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 2007 | { |
| 2008 | Ice::Type valueTy = T(valueType); |
| 2009 | switch(valueTy) |
| 2010 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2011 | case Ice::IceType_i32: |
| 2012 | return T(getNaturalIntType()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2013 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2014 | case Ice::IceType_f32: |
| 2015 | return T(Ice::IceType_f64); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2016 | |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2017 | default: |
| 2018 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 2019 | return {}; |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2023 | Value *Nucleus::createNullValue(Type *Ty) |
| 2024 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2025 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2026 | if(Ice::isVectorType(T(Ty))) |
| 2027 | { |
| 2028 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2029 | 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] | 2030 | return createConstantVector(c, Ty); |
| 2031 | } |
| 2032 | else |
| 2033 | { |
| 2034 | return V(::context->getConstantZero(T(Ty))); |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | Value *Nucleus::createConstantLong(int64_t 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->getConstantInt64(i)); |
| 2042 | } |
| 2043 | |
| 2044 | Value *Nucleus::createConstantInt(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::createConstantInt(unsigned int i) |
| 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->getConstantInt32(i)); |
| 2054 | } |
| 2055 | |
| 2056 | Value *Nucleus::createConstantBool(bool b) |
| 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->getConstantInt1(b)); |
| 2060 | } |
| 2061 | |
| 2062 | Value *Nucleus::createConstantByte(signed 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::createConstantByte(unsigned char 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->getConstantInt8(i)); |
| 2072 | } |
| 2073 | |
| 2074 | Value *Nucleus::createConstantShort(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::createConstantShort(unsigned short i) |
| 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->getConstantInt16(i)); |
| 2084 | } |
| 2085 | |
| 2086 | Value *Nucleus::createConstantFloat(float x) |
| 2087 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2088 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2089 | return V(::context->getConstantFloat(x)); |
| 2090 | } |
| 2091 | |
| 2092 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2093 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2094 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2095 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2096 | } |
| 2097 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2098 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2099 | { |
| 2100 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2101 | } |
| 2102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2103 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2104 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2105 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2106 | const int vectorSize = 16; |
| 2107 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2108 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2109 | |
| 2110 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2111 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2112 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2113 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2114 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2115 | |
| 2116 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2117 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2118 | case Ice::IceType_v4i32: |
| 2119 | case Ice::IceType_v4i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2120 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2121 | 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] | 2122 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2123 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2124 | } |
| 2125 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2126 | case Ice::IceType_v4f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2127 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2128 | 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] | 2129 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2130 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2131 | } |
| 2132 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2133 | case Ice::IceType_v8i16: |
| 2134 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2135 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2136 | 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] | 2137 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2138 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2139 | } |
| 2140 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2141 | case Ice::IceType_v16i8: |
| 2142 | case Ice::IceType_v16i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2143 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2144 | 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] | 2145 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2146 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2147 | } |
| 2148 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2149 | case Type_v2i32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2150 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2151 | 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] | 2152 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2153 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2154 | } |
| 2155 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2156 | case Type_v2f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2157 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2158 | 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] | 2159 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2160 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2161 | } |
| 2162 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2163 | case Type_v4i16: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2164 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2165 | 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] | 2166 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2167 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2168 | } |
| 2169 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2170 | case Type_v8i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2171 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2172 | 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] | 2173 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2174 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2175 | } |
| 2176 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2177 | case Type_v4i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2178 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2179 | 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] | 2180 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2181 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2182 | } |
| 2183 | break; |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 2184 | default: |
| 2185 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2186 | } |
| 2187 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2188 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2189 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2190 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2191 | return V(result); |
| 2192 | } |
| 2193 | |
| 2194 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2195 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2196 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2197 | } |
| 2198 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2199 | Value *Nucleus::createConstantString(const char *v) |
| 2200 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2201 | // 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] | 2202 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2203 | } |
| 2204 | |
Nicolas Capens | 54313fb | 2021-02-19 14:26:27 -0500 | [diff] [blame] | 2205 | void Nucleus::setOptimizerCallback(OptimizerCallback *callback) |
| 2206 | { |
| 2207 | ::optimizerCallback = callback; |
| 2208 | } |
| 2209 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2210 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2211 | { |
| 2212 | return T(Ice::IceType_void); |
| 2213 | } |
| 2214 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2215 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2216 | { |
| 2217 | return T(Ice::IceType_i1); |
| 2218 | } |
| 2219 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2220 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2221 | { |
| 2222 | return T(Ice::IceType_i8); |
| 2223 | } |
| 2224 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2225 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2226 | { |
| 2227 | return T(Ice::IceType_i8); |
| 2228 | } |
| 2229 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2230 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2231 | { |
| 2232 | return T(Ice::IceType_i16); |
| 2233 | } |
| 2234 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2235 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2236 | { |
| 2237 | return T(Ice::IceType_i16); |
| 2238 | } |
| 2239 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2240 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2241 | { |
| 2242 | return T(Type_v4i8); |
| 2243 | } |
| 2244 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2245 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2246 | { |
| 2247 | return T(Type_v4i8); |
| 2248 | } |
| 2249 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2250 | namespace { |
| 2251 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2252 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2253 | 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] | 2254 | } |
| 2255 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2256 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2257 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2258 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2262 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2263 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } // namespace |
| 2266 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2267 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2268 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2269 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2270 | if(emulateIntrinsics) |
| 2271 | { |
| 2272 | Byte8 result; |
| 2273 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2274 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2275 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2276 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2277 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2278 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2279 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2280 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2281 | |
| 2282 | return result; |
| 2283 | } |
| 2284 | else |
| 2285 | { |
| 2286 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2287 | 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] | 2288 | auto paddusb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2289 | paddusb->addArg(x.value()); |
| 2290 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2291 | ::basicBlock->appendInst(paddusb); |
| 2292 | |
| 2293 | return RValue<Byte8>(V(result)); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2298 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2299 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2300 | if(emulateIntrinsics) |
| 2301 | { |
| 2302 | Byte8 result; |
| 2303 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2304 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2305 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2306 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2307 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2308 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2309 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2310 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2311 | |
| 2312 | return result; |
| 2313 | } |
| 2314 | else |
| 2315 | { |
| 2316 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2317 | 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] | 2318 | auto psubusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2319 | psubusw->addArg(x.value()); |
| 2320 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2321 | ::basicBlock->appendInst(psubusw); |
| 2322 | |
| 2323 | return RValue<Byte8>(V(result)); |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | RValue<SByte> Extract(RValue<SByte8> val, 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<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2334 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2335 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2336 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2340 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2341 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2342 | if(emulateIntrinsics) |
| 2343 | { |
| 2344 | SByte8 result; |
| 2345 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2346 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2347 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2348 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2349 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2350 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2351 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2352 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2353 | |
| 2354 | return result; |
| 2355 | } |
| 2356 | else |
| 2357 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2358 | #if defined(__i386__) || defined(__x86_64__) |
| 2359 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2360 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2361 | 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] | 2362 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2363 | return As<SByte8>(hi | lo); |
| 2364 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2365 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2366 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2367 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2368 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2369 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2370 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2371 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2372 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2373 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2374 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2375 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2376 | 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] | 2377 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2378 | else |
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 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2381 | 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] | 2382 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2383 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2384 | ::basicBlock->appendInst(movmsk); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2385 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2386 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2387 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2388 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2389 | |
| 2390 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2391 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2392 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2393 | // } |
| 2394 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2395 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2396 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2397 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2398 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2399 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2400 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2401 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2402 | { |
| 2403 | return T(Type_v8i8); |
| 2404 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2405 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2406 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2407 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2408 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2409 | // } |
| 2410 | |
| 2411 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2412 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2413 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2414 | // } |
| 2415 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2416 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 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 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2420 | } |
| 2421 | |
| 2422 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2423 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2424 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2425 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2426 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2427 | SByte8 result; |
| 2428 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2429 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2430 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2431 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2432 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2433 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2434 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2435 | 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] | 2436 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2437 | return result; |
| 2438 | } |
| 2439 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2440 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2441 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2442 | 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] | 2443 | auto paddsb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2444 | paddsb->addArg(x.value()); |
| 2445 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2446 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2447 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2448 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2449 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2450 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2451 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2452 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2453 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2454 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2455 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2456 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2457 | SByte8 result; |
| 2458 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2459 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2460 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2461 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2462 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2463 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2464 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2465 | 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] | 2466 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2467 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2468 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2469 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2470 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2471 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2472 | 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] | 2473 | auto psubsb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2474 | psubsb->addArg(x.value()); |
| 2475 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2476 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2477 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2478 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2479 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2480 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2481 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2482 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2483 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2484 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2485 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2486 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2487 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2488 | 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] | 2489 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2490 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2491 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2492 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2493 | 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] | 2494 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2495 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2496 | ::basicBlock->appendInst(movmsk); |
| 2497 | |
| 2498 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2499 | } |
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> CmpGT(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>(createIntCompare(Ice::InstIcmp::Sgt, 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 | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2508 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2509 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2510 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2511 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2512 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2513 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2514 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2515 | { |
| 2516 | return T(Type_v8i8); |
| 2517 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2518 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2519 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2520 | { |
| 2521 | return T(Ice::IceType_v16i8); |
| 2522 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2523 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2524 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2525 | { |
| 2526 | return T(Ice::IceType_v16i8); |
| 2527 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2528 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2529 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2530 | { |
| 2531 | return T(Type_v2i16); |
| 2532 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2533 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2534 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2535 | { |
| 2536 | return T(Type_v2i16); |
| 2537 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2538 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2539 | Short4::Short4(RValue<Int4> cast) |
| 2540 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2541 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2542 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2543 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2544 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2545 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2546 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2547 | |
| 2548 | storeValue(short4); |
| 2549 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2550 | |
| 2551 | // Short4::Short4(RValue<Float> cast) |
| 2552 | // { |
| 2553 | // } |
| 2554 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2555 | Short4::Short4(RValue<Float4> cast) |
| 2556 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2557 | // TODO(b/150791192): Generalize and optimize |
| 2558 | auto smin = std::numeric_limits<short>::min(); |
| 2559 | auto smax = std::numeric_limits<short>::max(); |
| 2560 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2564 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2565 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2566 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2567 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2568 | Short4 result; |
| 2569 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2570 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2571 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2572 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2573 | |
| 2574 | return result; |
| 2575 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2576 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2577 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2578 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2579 | } |
| 2580 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2581 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2582 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2583 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2584 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2585 | if(emulateIntrinsics) |
| 2586 | { |
| 2587 | Short4 result; |
| 2588 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2589 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2590 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2591 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2592 | |
| 2593 | return result; |
| 2594 | } |
| 2595 | else |
| 2596 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2597 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2602 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2603 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2604 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2605 | 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] | 2606 | ::basicBlock->appendInst(cmp); |
| 2607 | |
| 2608 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2609 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2610 | ::basicBlock->appendInst(select); |
| 2611 | |
| 2612 | return RValue<Short4>(V(result)); |
| 2613 | } |
| 2614 | |
| 2615 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2616 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2617 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2618 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2619 | 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] | 2620 | ::basicBlock->appendInst(cmp); |
| 2621 | |
| 2622 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2623 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2624 | ::basicBlock->appendInst(select); |
| 2625 | |
| 2626 | return RValue<Short4>(V(result)); |
| 2627 | } |
| 2628 | |
| 2629 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 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 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2633 | } |
| 2634 | |
| 2635 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2636 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2637 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2638 | if(emulateIntrinsics) |
| 2639 | { |
| 2640 | Short4 result; |
| 2641 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2642 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2643 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2644 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2645 | |
| 2646 | return result; |
| 2647 | } |
| 2648 | else |
| 2649 | { |
| 2650 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2651 | 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] | 2652 | auto paddsw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2653 | paddsw->addArg(x.value()); |
| 2654 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2655 | ::basicBlock->appendInst(paddsw); |
| 2656 | |
| 2657 | return RValue<Short4>(V(result)); |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2662 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2663 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2664 | if(emulateIntrinsics) |
| 2665 | { |
| 2666 | Short4 result; |
| 2667 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2668 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2669 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2670 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2671 | |
| 2672 | return result; |
| 2673 | } |
| 2674 | else |
| 2675 | { |
| 2676 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2677 | 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] | 2678 | auto psubsw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2679 | psubsw->addArg(x.value()); |
| 2680 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2681 | ::basicBlock->appendInst(psubsw); |
| 2682 | |
| 2683 | return RValue<Short4>(V(result)); |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2688 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2689 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2690 | if(emulateIntrinsics) |
| 2691 | { |
| 2692 | Short4 result; |
| 2693 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2694 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2695 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2696 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2697 | |
| 2698 | return result; |
| 2699 | } |
| 2700 | else |
| 2701 | { |
| 2702 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2703 | 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] | 2704 | auto pmulhw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2705 | pmulhw->addArg(x.value()); |
| 2706 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2707 | ::basicBlock->appendInst(pmulhw); |
| 2708 | |
| 2709 | return RValue<Short4>(V(result)); |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2714 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2715 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2716 | if(emulateIntrinsics) |
| 2717 | { |
| 2718 | Int2 result; |
| 2719 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2720 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2721 | |
| 2722 | return result; |
| 2723 | } |
| 2724 | else |
| 2725 | { |
| 2726 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2727 | 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] | 2728 | auto pmaddwd = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2729 | pmaddwd->addArg(x.value()); |
| 2730 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2731 | ::basicBlock->appendInst(pmaddwd); |
| 2732 | |
| 2733 | return As<Int2>(V(result)); |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2738 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2739 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2740 | if(emulateIntrinsics) |
| 2741 | { |
| 2742 | SByte8 result; |
| 2743 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2744 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2745 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2746 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2747 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2748 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2749 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2750 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2751 | |
| 2752 | return result; |
| 2753 | } |
| 2754 | else |
| 2755 | { |
| 2756 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2757 | 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] | 2758 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2759 | pack->addArg(x.value()); |
| 2760 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2761 | ::basicBlock->appendInst(pack); |
| 2762 | |
| 2763 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2768 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2769 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2770 | if(emulateIntrinsics) |
| 2771 | { |
| 2772 | Byte8 result; |
| 2773 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2774 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2775 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2776 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2777 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2778 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2779 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2780 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2781 | |
| 2782 | return result; |
| 2783 | } |
| 2784 | else |
| 2785 | { |
| 2786 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2787 | 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] | 2788 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2789 | pack->addArg(x.value()); |
| 2790 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2791 | ::basicBlock->appendInst(pack); |
| 2792 | |
| 2793 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | RValue<Short4> CmpGT(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>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2804 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2805 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2806 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2807 | } |
| 2808 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2809 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2810 | { |
| 2811 | return T(Type_v4i16); |
| 2812 | } |
| 2813 | |
| 2814 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2815 | { |
| 2816 | if(saturate) |
| 2817 | { |
| 2818 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2819 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2820 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2821 | // PackUnsigned takes care of 0x0000 saturation. |
| 2822 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2823 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2824 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2825 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2826 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2827 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2828 | Int4 int4(cast); |
| 2829 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2830 | } |
| 2831 | else |
| 2832 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2833 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2834 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2835 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2836 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2837 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2838 | *this = Short4(Int4(cast)); |
| 2839 | } |
| 2840 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2841 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2842 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2843 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2844 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2848 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2849 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2853 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2854 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2855 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2856 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2857 | { |
| 2858 | UShort4 result; |
| 2859 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2860 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2861 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2862 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2863 | |
| 2864 | return result; |
| 2865 | } |
| 2866 | else |
| 2867 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2868 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2873 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2874 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2875 | if(emulateIntrinsics) |
| 2876 | { |
| 2877 | UShort4 result; |
| 2878 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2879 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2880 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2881 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2882 | |
| 2883 | return result; |
| 2884 | } |
| 2885 | else |
| 2886 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2887 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2892 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2893 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2894 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2895 | 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] | 2896 | ::basicBlock->appendInst(cmp); |
| 2897 | |
| 2898 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2899 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2900 | ::basicBlock->appendInst(select); |
| 2901 | |
| 2902 | return RValue<UShort4>(V(result)); |
| 2903 | } |
| 2904 | |
| 2905 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2906 | { |
| 2907 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2908 | 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] | 2909 | ::basicBlock->appendInst(cmp); |
| 2910 | |
| 2911 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2912 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2913 | ::basicBlock->appendInst(select); |
| 2914 | |
| 2915 | return RValue<UShort4>(V(result)); |
| 2916 | } |
| 2917 | |
| 2918 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 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 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2922 | } |
| 2923 | |
| 2924 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2925 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2926 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2927 | if(emulateIntrinsics) |
| 2928 | { |
| 2929 | UShort4 result; |
| 2930 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2931 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2932 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2933 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2934 | |
| 2935 | return result; |
| 2936 | } |
| 2937 | else |
| 2938 | { |
| 2939 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2940 | 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] | 2941 | auto paddusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2942 | paddusw->addArg(x.value()); |
| 2943 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2944 | ::basicBlock->appendInst(paddusw); |
| 2945 | |
| 2946 | return RValue<UShort4>(V(result)); |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2951 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2952 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2953 | if(emulateIntrinsics) |
| 2954 | { |
| 2955 | UShort4 result; |
| 2956 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2957 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2958 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2959 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2960 | |
| 2961 | return result; |
| 2962 | } |
| 2963 | else |
| 2964 | { |
| 2965 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2966 | 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] | 2967 | auto psubusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2968 | psubusw->addArg(x.value()); |
| 2969 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2970 | ::basicBlock->appendInst(psubusw); |
| 2971 | |
| 2972 | return RValue<UShort4>(V(result)); |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2977 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2978 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2979 | if(emulateIntrinsics) |
| 2980 | { |
| 2981 | UShort4 result; |
| 2982 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2983 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2984 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2985 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 2986 | |
| 2987 | return result; |
| 2988 | } |
| 2989 | else |
| 2990 | { |
| 2991 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2992 | 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] | 2993 | auto pmulhuw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2994 | pmulhuw->addArg(x.value()); |
| 2995 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2996 | ::basicBlock->appendInst(pmulhuw); |
| 2997 | |
| 2998 | return RValue<UShort4>(V(result)); |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3003 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3004 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3005 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3006 | |
| 3007 | // Scalarized implementation. |
| 3008 | Int4 result; |
| 3009 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 3010 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 3011 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 3012 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 3013 | |
| 3014 | return result; |
| 3015 | } |
| 3016 | |
| 3017 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3018 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3019 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3020 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3021 | |
| 3022 | if(false) // Partial product based implementation. |
| 3023 | { |
| 3024 | auto xh = x >> 16; |
| 3025 | auto yh = y >> 16; |
| 3026 | auto xl = x & UInt4(0x0000FFFF); |
| 3027 | auto yl = y & UInt4(0x0000FFFF); |
| 3028 | auto xlyh = xl * yh; |
| 3029 | auto xhyl = xh * yl; |
| 3030 | auto xlyhh = xlyh >> 16; |
| 3031 | auto xhylh = xhyl >> 16; |
| 3032 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3033 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3034 | auto xlylh = (xl * yl) >> 16; |
| 3035 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3036 | |
| 3037 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3038 | } |
| 3039 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3040 | // Scalarized implementation. |
| 3041 | Int4 result; |
| 3042 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3043 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3044 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3045 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3046 | |
| 3047 | return As<UInt4>(result); |
| 3048 | } |
| 3049 | |
| 3050 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3051 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3052 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3053 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3054 | return UShort4(0); |
| 3055 | } |
| 3056 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3057 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3058 | { |
| 3059 | return T(Type_v4i16); |
| 3060 | } |
| 3061 | |
| 3062 | RValue<Short> Extract(RValue<Short8> val, 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<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3069 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3071 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3075 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3076 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3077 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3078 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3079 | Short8 result; |
| 3080 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3081 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3082 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3083 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3084 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3085 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3086 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3087 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
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 | return result; |
| 3090 | } |
| 3091 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3092 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3093 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3094 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3095 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3096 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3097 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 3098 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3099 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3100 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3101 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3102 | Short8 result; |
| 3103 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 3104 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 3105 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 3106 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 3107 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 3108 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 3109 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 3110 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3111 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3112 | return result; |
| 3113 | } |
| 3114 | else |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3115 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3116 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3117 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3118 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3119 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3120 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3121 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3122 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3123 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3124 | return Int4(0); |
| 3125 | } |
| 3126 | |
| 3127 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 3128 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3129 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3130 | UNIMPLEMENTED_NO_BUG("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3131 | return Short8(0); |
| 3132 | } |
| 3133 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3134 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3135 | { |
| 3136 | return T(Ice::IceType_v8i16); |
| 3137 | } |
| 3138 | |
| 3139 | RValue<UShort> Extract(RValue<UShort8> val, 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<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3146 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3147 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3148 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3152 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3153 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3154 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3155 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3156 | UShort8 result; |
| 3157 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3158 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3159 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3160 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3161 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3162 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3163 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3164 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3165 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3166 | return result; |
| 3167 | } |
| 3168 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3169 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3170 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3171 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3172 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3173 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3174 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 3175 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3176 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3177 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3178 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3179 | UShort8 result; |
| 3180 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 3181 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 3182 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 3183 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 3184 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 3185 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 3186 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 3187 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3188 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3189 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3192 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3193 | return RValue<UShort8>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3194 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3195 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3196 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3197 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3198 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3199 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3200 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3201 | return UShort8(0); |
| 3202 | } |
| 3203 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3204 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3205 | { |
| 3206 | return T(Ice::IceType_v8i16); |
| 3207 | } |
| 3208 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3209 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3210 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3211 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3212 | RValue<Int> res = val; |
| 3213 | val += 1; |
| 3214 | return res; |
| 3215 | } |
| 3216 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3217 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3218 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3219 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3220 | val += 1; |
| 3221 | return val; |
| 3222 | } |
| 3223 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3224 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3225 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3226 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3227 | RValue<Int> res = val; |
| 3228 | val -= 1; |
| 3229 | return res; |
| 3230 | } |
| 3231 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3232 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3233 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3234 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3235 | val -= 1; |
| 3236 | return val; |
| 3237 | } |
| 3238 | |
| 3239 | RValue<Int> RoundInt(RValue<Float> cast) |
| 3240 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3241 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3242 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3243 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3244 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3245 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3246 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3247 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3248 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3249 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3250 | 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] | 3251 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3252 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3253 | ::basicBlock->appendInst(nearbyint); |
| 3254 | |
| 3255 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3256 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3257 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3258 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3259 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3260 | { |
| 3261 | return T(Ice::IceType_i32); |
| 3262 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3263 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3264 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3265 | { |
| 3266 | return T(Ice::IceType_i64); |
| 3267 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3268 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3269 | UInt::UInt(RValue<Float> cast) |
| 3270 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3271 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3272 | // Smallest positive value representable in UInt, but not in Int |
| 3273 | const unsigned int ustart = 0x80000000u; |
| 3274 | const float ustartf = float(ustart); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3275 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3276 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3277 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3278 | // Check if the value can be represented as an Int |
| 3279 | IfThenElse(cast >= ustartf, |
| 3280 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3281 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3282 | // Otherwise, just convert normally |
| 3283 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3284 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3285 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3286 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3287 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3288 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3289 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3290 | RValue<UInt> res = val; |
| 3291 | val += 1; |
| 3292 | return res; |
| 3293 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3294 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3295 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3296 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3297 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3298 | val += 1; |
| 3299 | return val; |
| 3300 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3301 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3302 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3305 | RValue<UInt> res = val; |
| 3306 | val -= 1; |
| 3307 | return res; |
| 3308 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3309 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3310 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3311 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3313 | val -= 1; |
| 3314 | return val; |
| 3315 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3316 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3317 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3318 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3319 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3320 | // } |
| 3321 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3322 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3323 | { |
| 3324 | return T(Ice::IceType_i32); |
| 3325 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3326 | |
| 3327 | // Int2::Int2(RValue<Int> cast) |
| 3328 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3329 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3330 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3331 | // |
| 3332 | // Constant *shuffle[2]; |
| 3333 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3334 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3335 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3336 | // 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] | 3337 | // |
| 3338 | // storeValue(replicate); |
| 3339 | // } |
| 3340 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3341 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3342 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3343 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3344 | if(emulateIntrinsics) |
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 | Int2 result; |
| 3347 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3348 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3349 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3350 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3353 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3354 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3355 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3356 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3357 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3358 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3359 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3361 | if(emulateIntrinsics) |
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 | Int2 result; |
| 3364 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3365 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3366 | |
| 3367 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3368 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3369 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3370 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3371 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3372 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3373 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3374 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3375 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3376 | { |
| 3377 | return T(Type_v2i32); |
| 3378 | } |
| 3379 | |
| 3380 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 3381 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3382 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3383 | if(emulateIntrinsics) |
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 | UInt2 result; |
| 3386 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3387 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3388 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3389 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3392 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3393 | return RValue<UInt2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3394 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3395 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3396 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3397 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3398 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3399 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3400 | if(emulateIntrinsics) |
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 | UInt2 result; |
| 3403 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3404 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3405 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3406 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3407 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3408 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3409 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3410 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3411 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3412 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3413 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3414 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3415 | { |
| 3416 | return T(Type_v2i32); |
| 3417 | } |
| 3418 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3419 | Int4::Int4(RValue<Byte4> cast) |
| 3420 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3421 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3422 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3423 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3424 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3425 | |
| 3426 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3427 | 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] | 3428 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3429 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3430 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3431 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3432 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3433 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3434 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3435 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3436 | storeValue(f); |
| 3437 | } |
| 3438 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3439 | Int4::Int4(RValue<SByte4> cast) |
| 3440 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3441 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3442 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3443 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3444 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3445 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3446 | 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] | 3447 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3448 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3449 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3450 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3451 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3452 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3453 | |
| 3454 | *this = As<Int4>(e) >> 24; |
| 3455 | } |
| 3456 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3457 | Int4::Int4(RValue<Short4> cast) |
| 3458 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3459 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3460 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3461 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3462 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3463 | |
| 3464 | *this = As<Int4>(c) >> 16; |
| 3465 | } |
| 3466 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3467 | Int4::Int4(RValue<UShort4> cast) |
| 3468 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3469 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3470 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3471 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3472 | 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] | 3473 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3474 | storeValue(d); |
| 3475 | } |
| 3476 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3477 | Int4::Int4(RValue<Int> rhs) |
| 3478 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3479 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3480 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3481 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3482 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3483 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3484 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3485 | |
| 3486 | storeValue(replicate); |
| 3487 | } |
| 3488 | |
| 3489 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3490 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3491 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3492 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3493 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3494 | Int4 result; |
| 3495 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3496 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3497 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3498 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3499 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3500 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3503 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3504 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3505 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3506 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3507 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3508 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3509 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3510 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3511 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3512 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3513 | Int4 result; |
| 3514 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3515 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3516 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3517 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3518 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3519 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3522 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3523 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3524 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3525 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3526 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3527 | RValue<Int4> CmpEQ(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::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | RValue<Int4> CmpLT(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::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | RValue<Int4> CmpLE(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::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | RValue<Int4> CmpNEQ(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::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | RValue<Int4> CmpNLT(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::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3558 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3559 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3560 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3561 | } |
| 3562 | |
Nicolas Capens | 629bf95 | 2022-01-18 15:08:14 -0500 | [diff] [blame] | 3563 | RValue<Int4> Abs(RValue<Int4> x) |
| 3564 | { |
| 3565 | // TODO: Optimize. |
| 3566 | auto negative = x >> 31; |
| 3567 | return (x ^ negative) - negative; |
| 3568 | } |
| 3569 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3570 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3571 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3572 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3573 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3574 | 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] | 3575 | ::basicBlock->appendInst(cmp); |
| 3576 | |
| 3577 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3578 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3579 | ::basicBlock->appendInst(select); |
| 3580 | |
| 3581 | return RValue<Int4>(V(result)); |
| 3582 | } |
| 3583 | |
| 3584 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3585 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3586 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3587 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3588 | 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] | 3589 | ::basicBlock->appendInst(cmp); |
| 3590 | |
| 3591 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3592 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3593 | ::basicBlock->appendInst(select); |
| 3594 | |
| 3595 | return RValue<Int4>(V(result)); |
| 3596 | } |
| 3597 | |
| 3598 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3599 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3600 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3601 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3602 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3603 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3604 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3605 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3606 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3607 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3608 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3609 | 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] | 3610 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3611 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3612 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3613 | |
| 3614 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3615 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3616 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3617 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3618 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3619 | { |
| 3620 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3621 | |
| 3622 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3623 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3624 | // saturate to 0x80000000. |
| 3625 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3626 | |
| 3627 | if(emulateIntrinsics || CPUID::ARM) |
| 3628 | { |
| 3629 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3630 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3631 | } |
| 3632 | else |
| 3633 | { |
| 3634 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3635 | 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] | 3636 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3637 | nearbyint->addArg(clamped.value()); |
| 3638 | ::basicBlock->appendInst(nearbyint); |
| 3639 | |
| 3640 | return RValue<Int4>(V(result)); |
| 3641 | } |
| 3642 | } |
| 3643 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3644 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3645 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3646 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3647 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3648 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3649 | Short8 result; |
| 3650 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3651 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3652 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3653 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3654 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3655 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3656 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3657 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3658 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3659 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3660 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3661 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3662 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3663 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3664 | 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] | 3665 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3666 | pack->addArg(x.value()); |
| 3667 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3668 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3669 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3670 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3671 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3672 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3673 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3674 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3675 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3676 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3677 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3678 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3679 | RValue<Int4> sx = As<Int4>(x); |
| 3680 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3681 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3682 | RValue<Int4> sy = As<Int4>(y); |
| 3683 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3684 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3685 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3686 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3687 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3688 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3689 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3690 | 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] | 3691 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3692 | pack->addArg(x.value()); |
| 3693 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3694 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3695 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3696 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3697 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3698 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3699 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3700 | RValue<Int> SignMask(RValue<Int4> x) |
| 3701 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3702 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3703 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3704 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3705 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3706 | 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] | 3707 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3708 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3709 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3710 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3711 | 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] | 3712 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3713 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3714 | ::basicBlock->appendInst(movmsk); |
| 3715 | |
| 3716 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3717 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3718 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3719 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3720 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3721 | { |
| 3722 | return T(Ice::IceType_v4i32); |
| 3723 | } |
| 3724 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3725 | UInt4::UInt4(RValue<Float4> cast) |
| 3726 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3727 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3728 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3729 | // Smallest positive value representable in UInt, but not in Int |
| 3730 | const unsigned int ustart = 0x80000000u; |
| 3731 | const float ustartf = float(ustart); |
| 3732 | |
| 3733 | // Check if the value can be represented as an Int |
| 3734 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3735 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3736 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3737 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3738 | (~uiValue & Int4(cast)); |
| 3739 | // 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] | 3740 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3741 | } |
| 3742 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3743 | UInt4::UInt4(RValue<UInt> rhs) |
| 3744 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3745 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3746 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3747 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3748 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3749 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3750 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3751 | |
| 3752 | storeValue(replicate); |
| 3753 | } |
| 3754 | |
| 3755 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3756 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3757 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3758 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3759 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3760 | UInt4 result; |
| 3761 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3762 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3763 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3764 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3765 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3766 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3767 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3768 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3769 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3770 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3771 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3772 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3773 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3774 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3775 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3776 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3777 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3778 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3779 | UInt4 result; |
| 3780 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3781 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3782 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3783 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3784 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3785 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3788 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3789 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3790 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3791 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3792 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3793 | RValue<UInt4> CmpEQ(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::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | RValue<UInt4> CmpLT(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::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | RValue<UInt4> CmpLE(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::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | RValue<UInt4> CmpNEQ(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::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | RValue<UInt4> CmpNLT(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::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | RValue<UInt4> CmpNLE(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 | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3826 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3830 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3831 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3832 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3833 | 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] | 3834 | ::basicBlock->appendInst(cmp); |
| 3835 | |
| 3836 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3837 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3838 | ::basicBlock->appendInst(select); |
| 3839 | |
| 3840 | return RValue<UInt4>(V(result)); |
| 3841 | } |
| 3842 | |
| 3843 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3844 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3845 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3846 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3847 | 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] | 3848 | ::basicBlock->appendInst(cmp); |
| 3849 | |
| 3850 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3851 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3852 | ::basicBlock->appendInst(select); |
| 3853 | |
| 3854 | return RValue<UInt4>(V(result)); |
| 3855 | } |
| 3856 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3857 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3858 | { |
| 3859 | return T(Ice::IceType_v4i32); |
| 3860 | } |
| 3861 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3862 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3863 | { |
| 3864 | return T(Ice::IceType_i16); |
| 3865 | } |
| 3866 | |
| 3867 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 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 1.0f / x; |
| 3871 | } |
| 3872 | |
| 3873 | RValue<Float> RcpSqrt_pp(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 | return Rcp_pp(Sqrt(x)); |
| 3877 | } |
| 3878 | |
| 3879 | RValue<Float> Sqrt(RValue<Float> x) |
| 3880 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3881 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3882 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3883 | 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] | 3884 | auto sqrt = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3885 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3886 | ::basicBlock->appendInst(sqrt); |
| 3887 | |
| 3888 | return RValue<Float>(V(result)); |
| 3889 | } |
| 3890 | |
| 3891 | RValue<Float> Round(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(Round(Float4(x))).x; |
| 3895 | } |
| 3896 | |
| 3897 | RValue<Float> Trunc(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(Trunc(Float4(x))).x; |
| 3901 | } |
| 3902 | |
| 3903 | RValue<Float> Frac(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(Frac(Float4(x))).x; |
| 3907 | } |
| 3908 | |
| 3909 | RValue<Float> Floor(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(Floor(Float4(x))).x; |
| 3913 | } |
| 3914 | |
| 3915 | RValue<Float> Ceil(RValue<Float> x) |
| 3916 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3917 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3918 | return Float4(Ceil(Float4(x))).x; |
| 3919 | } |
| 3920 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3921 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3922 | { |
| 3923 | return T(Ice::IceType_f32); |
| 3924 | } |
| 3925 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3926 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3927 | { |
| 3928 | return T(Type_v2f32); |
| 3929 | } |
| 3930 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3931 | Float4::Float4(RValue<Float> rhs) |
| 3932 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3933 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3934 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3935 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3936 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3937 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3938 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3939 | |
| 3940 | storeValue(replicate); |
| 3941 | } |
| 3942 | |
Nicolas Capens | bc74bc2 | 2022-01-26 10:47:00 -0500 | [diff] [blame] | 3943 | RValue<Float4> MulAdd(RValue<Float4> x, RValue<Float4> y, RValue<Float4> z) |
| 3944 | { |
| 3945 | // TODO(b/214591655): Use FMA when available. |
| 3946 | return x * y + z; |
| 3947 | } |
| 3948 | |
Nicolas Capens | 75d79f2 | 2022-01-31 17:46:26 -0500 | [diff] [blame] | 3949 | RValue<Float4> FMA(RValue<Float4> x, RValue<Float4> y, RValue<Float4> z) |
| 3950 | { |
| 3951 | // TODO(b/214591655): Use FMA instructions when available. |
| 3952 | return emulated::FMA(x, y, z); |
| 3953 | } |
| 3954 | |
Nicolas Capens | 629bf95 | 2022-01-18 15:08:14 -0500 | [diff] [blame] | 3955 | RValue<Float4> Abs(RValue<Float4> x) |
| 3956 | { |
| 3957 | // TODO: Optimize. |
| 3958 | Value *vector = Nucleus::createBitCast(x.value(), Int4::type()); |
| 3959 | int64_t constantVector[4] = { 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF }; |
| 3960 | Value *result = Nucleus::createAnd(vector, Nucleus::createConstantVector(constantVector, Int4::type())); |
| 3961 | |
| 3962 | return As<Float4>(result); |
| 3963 | } |
| 3964 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3965 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3966 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3967 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3968 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3969 | 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] | 3970 | ::basicBlock->appendInst(cmp); |
| 3971 | |
| 3972 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3973 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3974 | ::basicBlock->appendInst(select); |
| 3975 | |
| 3976 | return RValue<Float4>(V(result)); |
| 3977 | } |
| 3978 | |
| 3979 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3980 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3981 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3982 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3983 | 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] | 3984 | ::basicBlock->appendInst(cmp); |
| 3985 | |
| 3986 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3987 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3988 | ::basicBlock->appendInst(select); |
| 3989 | |
| 3990 | return RValue<Float4>(V(result)); |
| 3991 | } |
| 3992 | |
| 3993 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3994 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3995 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3996 | return Float4(1.0f) / x; |
| 3997 | } |
| 3998 | |
| 3999 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 4000 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4001 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4002 | return Rcp_pp(Sqrt(x)); |
| 4003 | } |
| 4004 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 4005 | bool HasRcpApprox() |
| 4006 | { |
| 4007 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4008 | return false; |
| 4009 | } |
| 4010 | |
| 4011 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 4012 | { |
| 4013 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4014 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 4015 | return { 0.0f }; |
| 4016 | } |
| 4017 | |
| 4018 | RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2) |
| 4019 | { |
| 4020 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4021 | UNREACHABLE("RValue<Float> RcpApprox()"); |
| 4022 | return { 0.0f }; |
| 4023 | } |
| 4024 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 4025 | bool HasRcpSqrtApprox() |
| 4026 | { |
| 4027 | return false; |
| 4028 | } |
| 4029 | |
| 4030 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 4031 | { |
| 4032 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4033 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 4034 | return { 0.0f }; |
| 4035 | } |
| 4036 | |
| 4037 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 4038 | { |
| 4039 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4040 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 4041 | return { 0.0f }; |
| 4042 | } |
| 4043 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4044 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4045 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4046 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4047 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4048 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4049 | Float4 result; |
| 4050 | result.x = Sqrt(Float(Float4(x).x)); |
| 4051 | result.y = Sqrt(Float(Float4(x).y)); |
| 4052 | result.z = Sqrt(Float(Float4(x).z)); |
| 4053 | result.w = Sqrt(Float(Float4(x).w)); |
| 4054 | |
| 4055 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4056 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4057 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4058 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4059 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4060 | 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] | 4061 | auto sqrt = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4062 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4063 | ::basicBlock->appendInst(sqrt); |
| 4064 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4065 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4066 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4067 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4068 | |
| 4069 | RValue<Int> SignMask(RValue<Float4> x) |
| 4070 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4071 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4072 | if(emulateIntrinsics || CPUID::ARM) |
| 4073 | { |
| 4074 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4075 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4076 | } |
| 4077 | else |
| 4078 | { |
| 4079 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4080 | 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] | 4081 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4082 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4083 | ::basicBlock->appendInst(movmsk); |
| 4084 | |
| 4085 | return RValue<Int>(V(result)); |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | RValue<Int4> CmpEQ(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::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | RValue<Int4> CmpLT(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::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | RValue<Int4> CmpLE(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::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | RValue<Int4> CmpNEQ(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::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | RValue<Int4> CmpNLT(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::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | RValue<Int4> CmpNLE(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::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | RValue<Int4> CmpUEQ(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::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | RValue<Int4> CmpULT(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::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | RValue<Int4> CmpULE(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::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | RValue<Int4> CmpUNEQ(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::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4150 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4151 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4152 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4156 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4157 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4158 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | RValue<Float4> Round(RValue<Float4> x) |
| 4162 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4163 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4164 | if(emulateIntrinsics || CPUID::ARM) |
| 4165 | { |
| 4166 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4167 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4168 | } |
| 4169 | else if(CPUID::SSE4_1) |
| 4170 | { |
| 4171 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4172 | 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] | 4173 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4174 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4175 | round->addArg(::context->getConstantInt32(0)); |
| 4176 | ::basicBlock->appendInst(round); |
| 4177 | |
| 4178 | return RValue<Float4>(V(result)); |
| 4179 | } |
| 4180 | else |
| 4181 | { |
| 4182 | return Float4(RoundInt(x)); |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | RValue<Float4> Trunc(RValue<Float4> x) |
| 4187 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4188 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4189 | if(CPUID::SSE4_1) |
| 4190 | { |
| 4191 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4192 | 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] | 4193 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4194 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4195 | round->addArg(::context->getConstantInt32(3)); |
| 4196 | ::basicBlock->appendInst(round); |
| 4197 | |
| 4198 | return RValue<Float4>(V(result)); |
| 4199 | } |
| 4200 | else |
| 4201 | { |
| 4202 | return Float4(Int4(x)); |
| 4203 | } |
| 4204 | } |
| 4205 | |
| 4206 | RValue<Float4> Frac(RValue<Float4> x) |
| 4207 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4208 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4209 | Float4 frc; |
| 4210 | |
| 4211 | if(CPUID::SSE4_1) |
| 4212 | { |
| 4213 | frc = x - Floor(x); |
| 4214 | } |
| 4215 | else |
| 4216 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4217 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4218 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4219 | 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] | 4220 | } |
| 4221 | |
| 4222 | // x - floor(x) can be 1.0 for very small negative x. |
| 4223 | // Clamp against the value just below 1.0. |
| 4224 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4225 | } |
| 4226 | |
| 4227 | RValue<Float4> Floor(RValue<Float4> x) |
| 4228 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4229 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4230 | if(CPUID::SSE4_1) |
| 4231 | { |
| 4232 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4233 | 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] | 4234 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4235 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4236 | round->addArg(::context->getConstantInt32(1)); |
| 4237 | ::basicBlock->appendInst(round); |
| 4238 | |
| 4239 | return RValue<Float4>(V(result)); |
| 4240 | } |
| 4241 | else |
| 4242 | { |
| 4243 | return x - Frac(x); |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4248 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4249 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4250 | if(CPUID::SSE4_1) |
| 4251 | { |
| 4252 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4253 | 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] | 4254 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4255 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4256 | round->addArg(::context->getConstantInt32(2)); |
| 4257 | ::basicBlock->appendInst(round); |
| 4258 | |
| 4259 | return RValue<Float4>(V(result)); |
| 4260 | } |
| 4261 | else |
| 4262 | { |
| 4263 | return -Floor(-x); |
| 4264 | } |
| 4265 | } |
| 4266 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4267 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4268 | { |
| 4269 | return T(Ice::IceType_v4f32); |
| 4270 | } |
| 4271 | |
| 4272 | RValue<Long> Ticks() |
| 4273 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4274 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4275 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4276 | return Long(Int(0)); |
| 4277 | } |
| 4278 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4279 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
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(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4282 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4283 | } |
| 4284 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4285 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4286 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4287 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4288 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4289 | } |
| 4290 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4291 | 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] | 4292 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4293 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4294 | 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] | 4295 | } |
| 4296 | |
| 4297 | void Breakpoint() |
| 4298 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4299 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4300 | 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] | 4301 | auto trap = Ice::InstIntrinsic::create(::function, 0, nullptr, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4302 | ::basicBlock->appendInst(trap); |
| 4303 | } |
| 4304 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4305 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4306 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4307 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4308 | 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] | 4309 | auto inst = Ice::InstIntrinsic::create(::function, 0, nullptr, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4310 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4311 | inst->addArg(order); |
| 4312 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4313 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4314 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4315 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4316 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4317 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame] | 4318 | UNIMPLEMENTED("b/155867273 Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4319 | return nullptr; |
| 4320 | } |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame] | 4321 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4322 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4323 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4324 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e4b7794 | 2021-08-03 17:09:41 -0400 | [diff] [blame] | 4325 | UNIMPLEMENTED("b/155867273 Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4326 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4327 | |
| 4328 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 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::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4332 | } |
| 4333 | |
| 4334 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 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::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4338 | } |
| 4339 | |
| 4340 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 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::Scatter(base, val, offsets, mask, alignment); |
| 4344 | } |
| 4345 | |
| 4346 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 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::Scatter(base, val, offsets, mask, alignment); |
| 4350 | } |
| 4351 | |
| 4352 | RValue<Float> Exp2(RValue<Float> x) |
| 4353 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4355 | return emulated::Exp2(x); |
| 4356 | } |
| 4357 | |
| 4358 | RValue<Float> Log2(RValue<Float> x) |
| 4359 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4361 | return emulated::Log2(x); |
| 4362 | } |
| 4363 | |
| 4364 | RValue<Float4> Sin(RValue<Float4> x) |
| 4365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4367 | return emulated::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | RValue<Float4> Cos(RValue<Float4> x) |
| 4371 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4372 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4373 | return emulated::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | RValue<Float4> Tan(RValue<Float4> x) |
| 4377 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4378 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4379 | return emulated::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4380 | } |
| 4381 | |
Nicolas Capens | d04f3f5 | 2022-02-05 01:19:14 -0500 | [diff] [blame] | 4382 | RValue<Float4> Asin(RValue<Float4> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4383 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4384 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4385 | return emulated::Asin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4386 | } |
| 4387 | |
Nicolas Capens | d04f3f5 | 2022-02-05 01:19:14 -0500 | [diff] [blame] | 4388 | RValue<Float4> Acos(RValue<Float4> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4389 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4390 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4391 | return emulated::Acos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | RValue<Float4> Atan(RValue<Float4> x) |
| 4395 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4396 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4397 | return emulated::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4398 | } |
| 4399 | |
| 4400 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4401 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4402 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4403 | return emulated::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | RValue<Float4> Cosh(RValue<Float4> x) |
| 4407 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4408 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4409 | return emulated::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | RValue<Float4> Tanh(RValue<Float4> x) |
| 4413 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4415 | return emulated::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | RValue<Float4> Asinh(RValue<Float4> x) |
| 4419 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4421 | return emulated::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | RValue<Float4> Acosh(RValue<Float4> x) |
| 4425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4427 | return emulated::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4431 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4432 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4433 | return emulated::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | RValue<Float4> Atan2(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(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4439 | return emulated::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4445 | return emulated::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | RValue<Float4> Exp(RValue<Float4> x) |
| 4449 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4450 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4451 | return emulated::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | RValue<Float4> Log(RValue<Float4> x) |
| 4455 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4457 | return emulated::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4461 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4463 | return emulated::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | RValue<Float4> Log2(RValue<Float4> x) |
| 4467 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4468 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | f7c42b0 | 2022-02-05 00:31:49 -0500 | [diff] [blame] | 4469 | return emulated::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4473 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4474 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4475 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4476 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4477 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4478 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4479 | } |
| 4480 | else |
| 4481 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4482 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4483 | 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] | 4484 | auto ctlz = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4485 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4486 | ::basicBlock->appendInst(ctlz); |
| 4487 | |
| 4488 | return RValue<UInt>(V(result)); |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4493 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4494 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4495 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4496 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4497 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4498 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4499 | } |
| 4500 | else |
| 4501 | { |
| 4502 | // TODO: implement vectorized version in Subzero |
| 4503 | UInt4 result; |
| 4504 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4505 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4506 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4507 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4508 | return result; |
| 4509 | } |
| 4510 | } |
| 4511 | |
| 4512 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4513 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4514 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4515 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4516 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4517 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4518 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4519 | } |
| 4520 | else |
| 4521 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4522 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4523 | 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] | 4524 | auto ctlz = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4525 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4526 | ::basicBlock->appendInst(ctlz); |
| 4527 | |
| 4528 | return RValue<UInt>(V(result)); |
| 4529 | } |
| 4530 | } |
| 4531 | |
| 4532 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4533 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4534 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4535 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4536 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4537 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4538 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4539 | } |
| 4540 | else |
| 4541 | { |
| 4542 | // TODO: implement vectorized version in Subzero |
| 4543 | UInt4 result; |
| 4544 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4545 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4546 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4547 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4548 | return result; |
| 4549 | } |
| 4550 | } |
| 4551 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4552 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> 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<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> 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::MinAtomic(x, y, memoryOrder); |
| 4562 | } |
| 4563 | |
| 4564 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> 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 | |
| 4570 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4571 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4572 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4573 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4574 | } |
| 4575 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4576 | void EmitDebugLocation() |
| 4577 | { |
| 4578 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4579 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4580 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4581 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4582 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4583 | void FlushDebug() {} |
| 4584 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4585 | namespace { |
| 4586 | namespace coro { |
| 4587 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4588 | // Instance data per generated coroutine |
| 4589 | // This is the "handle" type used for Coroutine functions |
| 4590 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4591 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4592 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4593 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4594 | bool done = false; // the coroutine should stop at the next yield() |
| 4595 | bool terminated = false; // the coroutine has finished. |
| 4596 | bool inRoutine = false; // is the coroutine currently executing? |
| 4597 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4598 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4599 | void *promisePtr = nullptr; |
| 4600 | }; |
| 4601 | |
| 4602 | CoroutineData *createCoroutineData() |
| 4603 | { |
| 4604 | return new CoroutineData{}; |
| 4605 | } |
| 4606 | |
| 4607 | void destroyCoroutineData(CoroutineData *coroData) |
| 4608 | { |
| 4609 | delete coroData; |
| 4610 | } |
| 4611 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4612 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4613 | // caller's call to await(). |
| 4614 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4615 | // is called. |
| 4616 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4617 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4618 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4619 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4620 | ASSERT(coroData->inRoutine); |
| 4621 | coroData->inRoutine = false; |
| 4622 | coroData->mainFiber->notify(); |
| 4623 | while(!coroData->inRoutine) |
| 4624 | { |
| 4625 | coroData->routineFiber->wait(); |
| 4626 | } |
| 4627 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4628 | } |
| 4629 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4630 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4631 | // or the coroutine terminates. |
| 4632 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4633 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4634 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4635 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4636 | ASSERT(!coroData->inRoutine); |
| 4637 | coroData->inRoutine = true; |
| 4638 | coroData->routineFiber->notify(); |
| 4639 | while(coroData->inRoutine) |
| 4640 | { |
| 4641 | coroData->mainFiber->wait(); |
| 4642 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4643 | } |
| 4644 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4645 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4646 | // until the coroutine ends, and deletes the coroutine data. |
| 4647 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4648 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4649 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4650 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4651 | ASSERT(!coroData->inRoutine); |
| 4652 | if(!coroData->terminated) |
| 4653 | { |
| 4654 | coroData->done = true; |
| 4655 | coroData->inRoutine = true; |
| 4656 | coroData->routineFiber->notify(); |
| 4657 | while(!coroData->terminated) |
| 4658 | { |
| 4659 | coroData->mainFiber->wait(); |
| 4660 | } |
| 4661 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4662 | if(coroData->useInternalScheduler) |
| 4663 | { |
| 4664 | ::getOrCreateScheduler().unbind(); |
| 4665 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4666 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | namespace detail { |
| 4670 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4671 | } // namespace detail |
| 4672 | |
| 4673 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4674 | { |
| 4675 | ASSERT(!detail::coroHandle); |
| 4676 | detail::coroHandle = handle; |
| 4677 | } |
| 4678 | |
| 4679 | Nucleus::CoroutineHandle getHandleParam() |
| 4680 | { |
| 4681 | ASSERT(detail::coroHandle); |
| 4682 | auto handle = detail::coroHandle; |
| 4683 | detail::coroHandle = {}; |
| 4684 | return handle; |
| 4685 | } |
| 4686 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4687 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4688 | { |
| 4689 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4690 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4694 | { |
| 4695 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4696 | coroData->promisePtr = promisePtr; |
| 4697 | } |
| 4698 | |
| 4699 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4700 | { |
| 4701 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4702 | return coroData->promisePtr; |
| 4703 | } |
| 4704 | |
| 4705 | } // namespace coro |
| 4706 | } // namespace |
| 4707 | |
| 4708 | // Used to generate coroutines. |
| 4709 | // Lifetime: from yield to acquireCoroutine |
| 4710 | class CoroutineGenerator |
| 4711 | { |
| 4712 | public: |
| 4713 | CoroutineGenerator() |
| 4714 | { |
| 4715 | } |
| 4716 | |
| 4717 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4718 | void generateCoroutineBegin() |
| 4719 | { |
| 4720 | // Begin building the main coroutine_begin() function. |
| 4721 | // We insert these instructions at the top of the entry node, |
| 4722 | // before existing reactor-generated instructions. |
| 4723 | |
| 4724 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4725 | // { |
| 4726 | // this->handle = coro::getHandleParam(); |
| 4727 | // |
| 4728 | // YieldType promise; |
| 4729 | // coro::setPromisePtr(handle, &promise); // For await |
| 4730 | // |
| 4731 | // ... <REACTOR CODE> ... |
| 4732 | // |
| 4733 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4734 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4735 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4736 | |
| 4737 | // YieldType promise; |
| 4738 | // coro::setPromisePtr(handle, &promise); // For await |
| 4739 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4740 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4744 | void generateYield(Value *val) |
| 4745 | { |
| 4746 | // ... <REACTOR CODE> ... |
| 4747 | // |
| 4748 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4749 | // if (!coro::suspend(handle)) { |
| 4750 | // return false; // coroutine has been stopped by the caller. |
| 4751 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4752 | // |
| 4753 | // ... <REACTOR CODE> ... |
| 4754 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4755 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4756 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4757 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4758 | // if (!coro::suspend(handle)) { |
| 4759 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4760 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4761 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4762 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4763 | |
| 4764 | // return false; // coroutine has been stopped by the caller. |
| 4765 | ::basicBlock = doneBlock; |
| 4766 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4767 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4768 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4769 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4773 | |
| 4774 | // Generates the await function for the current coroutine. |
| 4775 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4776 | static FunctionUniquePtr generateAwaitFunction() |
| 4777 | { |
| 4778 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4779 | // { |
| 4780 | // if (coro::isDone()) |
| 4781 | // { |
| 4782 | // return false; |
| 4783 | // } |
| 4784 | // else // resume |
| 4785 | // { |
| 4786 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4787 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4788 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4789 | // return true; |
| 4790 | // } |
| 4791 | // } |
| 4792 | |
| 4793 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4794 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4795 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4796 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4797 | |
| 4798 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4799 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4800 | |
| 4801 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4802 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4803 | |
| 4804 | auto doneBlock = awaitFunc->makeNode(); |
| 4805 | { |
| 4806 | // return false; |
| 4807 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4808 | doneBlock->appendInst(ret); |
| 4809 | } |
| 4810 | |
| 4811 | auto resumeBlock = awaitFunc->makeNode(); |
| 4812 | { |
| 4813 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4814 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4815 | |
| 4816 | // *out = *promise; |
| 4817 | // Load promise value |
| 4818 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4819 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4820 | resumeBlock->appendInst(load); |
| 4821 | // Then store it in output param |
| 4822 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4823 | resumeBlock->appendInst(store); |
| 4824 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4825 | // coro::resume(handle); |
| 4826 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4827 | |
| 4828 | // return true; |
| 4829 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4830 | resumeBlock->appendInst(ret); |
| 4831 | } |
| 4832 | |
| 4833 | // if (coro::isDone()) |
| 4834 | // { |
| 4835 | // <doneBlock> |
| 4836 | // } |
| 4837 | // else // resume |
| 4838 | // { |
| 4839 | // <resumeBlock> |
| 4840 | // } |
| 4841 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4842 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4843 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4844 | bb->appendInst(br); |
| 4845 | |
| 4846 | return FunctionUniquePtr{ awaitFunc }; |
| 4847 | } |
| 4848 | |
| 4849 | // Generates the destroy function for the current coroutine. |
| 4850 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4851 | static FunctionUniquePtr generateDestroyFunction() |
| 4852 | { |
| 4853 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4854 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4855 | // 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] | 4856 | // return; |
| 4857 | // } |
| 4858 | |
| 4859 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4860 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4861 | |
| 4862 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4863 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4864 | |
| 4865 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4866 | |
| 4867 | auto *bb = destroyFunc->getEntryNode(); |
| 4868 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4869 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4870 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4871 | |
| 4872 | // return; |
| 4873 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4874 | bb->appendInst(ret); |
| 4875 | |
| 4876 | return FunctionUniquePtr{ destroyFunc }; |
| 4877 | } |
| 4878 | |
| 4879 | private: |
| 4880 | Ice::Variable *handle{}; |
| 4881 | Ice::Variable *promise{}; |
| 4882 | }; |
| 4883 | |
| 4884 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4885 | { |
| 4886 | // This doubles up as our coroutine handle |
| 4887 | auto coroData = coro::createCoroutineData(); |
| 4888 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4889 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4890 | if(coroData->useInternalScheduler) |
| 4891 | { |
| 4892 | ::getOrCreateScheduler().bind(); |
| 4893 | } |
| 4894 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4895 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4896 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4897 | // any fiber switch occurs. |
| 4898 | coro::setHandleParam(coroData); |
| 4899 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4900 | ASSERT(!coroData->routineFiber); |
| 4901 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4902 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4903 | beginFunc(); |
| 4904 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4905 | ASSERT(coroData->inRoutine); |
| 4906 | coroData->done = true; // coroutine is done. |
| 4907 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4908 | coroData->inRoutine = false; |
| 4909 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4910 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4911 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4912 | ASSERT(!coroData->mainFiber); |
| 4913 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4914 | |
| 4915 | // block until the first yield or coroutine end |
| 4916 | ASSERT(!coroData->inRoutine); |
| 4917 | coroData->inRoutine = true; |
| 4918 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4919 | while(coroData->inRoutine) |
| 4920 | { |
| 4921 | coroData->mainFiber->wait(); |
| 4922 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4923 | |
| 4924 | return coroData; |
| 4925 | } |
| 4926 | |
| 4927 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4928 | { |
| 4929 | // Start by creating a regular function |
| 4930 | createFunction(yieldType, params); |
| 4931 | |
| 4932 | // Save in case yield() is called |
| 4933 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4934 | ::coroYieldType = yieldType; |
| 4935 | } |
| 4936 | |
| 4937 | void Nucleus::yield(Value *val) |
| 4938 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4939 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4940 | Variable::materializeAll(); |
| 4941 | |
| 4942 | // On first yield, we start generating coroutine functions |
| 4943 | if(!::coroGen) |
| 4944 | { |
| 4945 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4946 | ::coroGen->generateCoroutineBegin(); |
| 4947 | } |
| 4948 | |
| 4949 | ASSERT(::coroGen); |
| 4950 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4951 | } |
| 4952 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4953 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4954 | { |
| 4955 | return false; |
| 4956 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4957 | |
| 4958 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4959 | { |
| 4960 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4961 | |
Sean Risser | 705231f | 2021-08-19 18:17:24 -0400 | [diff] [blame] | 4962 | 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] | 4963 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4964 | if(::coroGen) |
| 4965 | { |
| 4966 | // Finish generating coroutine functions |
| 4967 | { |
| 4968 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4969 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4970 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4971 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4972 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4973 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4974 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4975 | // At this point, we no longer need the CoroutineGenerator. |
| 4976 | ::coroGen.reset(); |
| 4977 | ::coroYieldType = nullptr; |
| 4978 | |
| 4979 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4980 | { name, "await", "destroy" }, |
| 4981 | cfgEdit); |
| 4982 | |
| 4983 | return routine; |
| 4984 | } |
| 4985 | else |
| 4986 | { |
| 4987 | { |
| 4988 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4989 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | ::coroYieldType = nullptr; |
| 4993 | |
| 4994 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 4995 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 4996 | |
| 4997 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 4998 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 4999 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 5000 | return routine; |
| 5001 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5002 | } |
| 5003 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5004 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5005 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5006 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 5007 | |
| 5008 | if(isCoroutine) |
| 5009 | { |
| 5010 | return rr::invokeCoroutineBegin(func); |
| 5011 | } |
| 5012 | else |
| 5013 | { |
| 5014 | // For regular routines, just invoke the begin func directly |
| 5015 | return func(); |
| 5016 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5017 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5018 | |
| 5019 | } // namespace rr |