Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 15 | #include "Debug.hpp" |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 16 | #include "EmulatedIntrinsics.hpp" |
| 17 | #include "OptimalIntrinsics.hpp" |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 18 | #include "Print.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 19 | #include "Reactor.hpp" |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 20 | #include "ReactorDebugInfo.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 22 | #include "ExecutableMemory.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 23 | #include "Optimizer.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 25 | #include "src/IceCfg.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 26 | #include "src/IceCfgNode.h" |
| 27 | #include "src/IceELFObjectWriter.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 28 | #include "src/IceELFStreamer.h" |
| 29 | #include "src/IceGlobalContext.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 30 | #include "src/IceGlobalInits.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 31 | #include "src/IceTypes.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 32 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 34 | #include "llvm/Support/FileSystem.h" |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 36 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 37 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 38 | #include "marl/event.h" |
| 39 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 40 | #if __has_feature(memory_sanitizer) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 41 | # include <sanitizer/msan_interface.h> |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 42 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 43 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 44 | #if defined(_WIN32) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 45 | # ifndef WIN32_LEAN_AND_MEAN |
| 46 | # define WIN32_LEAN_AND_MEAN |
| 47 | # endif // !WIN32_LEAN_AND_MEAN |
| 48 | # ifndef NOMINMAX |
| 49 | # define NOMINMAX |
| 50 | # endif // !NOMINMAX |
| 51 | # include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 52 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 53 | |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 54 | #include <array> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 55 | #include <iostream> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 56 | #include <limits> |
| 57 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 58 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 59 | // Subzero utility functions |
| 60 | // These functions only accept and return Subzero (Ice) types, and do not access any globals. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 61 | namespace { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 62 | namespace sz { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 63 | |
| 64 | Ice::Cfg *createFunction(Ice::GlobalContext *context, Ice::Type returnType, const std::vector<Ice::Type> ¶mTypes) |
| 65 | { |
| 66 | uint32_t sequenceNumber = 0; |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame] | 67 | auto *function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 68 | |
| 69 | function->setStackSizeLimit(512 * 1024); // 512 KiB |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 70 | |
| 71 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 72 | |
| 73 | for(auto type : paramTypes) |
| 74 | { |
| 75 | Ice::Variable *arg = function->makeVariable(type); |
| 76 | function->addArg(arg); |
| 77 | } |
| 78 | |
| 79 | Ice::CfgNode *node = function->makeNode(); |
| 80 | function->setEntryNode(node); |
| 81 | |
| 82 | return function; |
| 83 | } |
| 84 | |
| 85 | Ice::Type getPointerType(Ice::Type elementType) |
| 86 | { |
| 87 | if(sizeof(void *) == 8) |
| 88 | { |
| 89 | return Ice::IceType_i64; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | return Ice::IceType_i32; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 98 | { |
| 99 | int typeSize = Ice::typeWidthInBytes(type); |
| 100 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 101 | |
| 102 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 103 | auto address = function->makeVariable(getPointerType(type)); |
| 104 | auto alloca = Ice::InstAlloca::create(function, address, bytes, typeSize); |
| 105 | function->getEntryNode()->getInsts().push_front(alloca); |
| 106 | |
| 107 | return address; |
| 108 | } |
| 109 | |
| 110 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 111 | { |
| 112 | if(sizeof(void *) == 8) |
| 113 | { |
| 114 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 119 | } |
| 120 | } |
| 121 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 122 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 123 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 124 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 125 | // Wrapper for calls on C functions with Ice types |
| 126 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 127 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 128 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 129 | |
| 130 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 131 | // then truncate result to bool. |
| 132 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 133 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 134 | if(returningBool) |
| 135 | { |
| 136 | ret = function->makeVariable(Ice::IceType_i32); |
| 137 | } |
| 138 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 139 | { |
| 140 | ret = function->makeVariable(retTy); |
| 141 | } |
| 142 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 143 | auto call = Ice::InstCall::create(function, iceArgs.size(), ret, callTarget, false, false, isVariadic); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 144 | for(auto arg : iceArgs) |
| 145 | { |
| 146 | call->addArg(arg); |
| 147 | } |
| 148 | |
| 149 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 150 | |
| 151 | if(returningBool) |
| 152 | { |
| 153 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 154 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 155 | } |
| 156 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 157 | return ret; |
| 158 | } |
| 159 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 160 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 161 | { |
| 162 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 163 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 164 | } |
| 165 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 166 | // Wrapper for calls on C functions with Ice types |
| 167 | template<typename Return, typename... CArgs, typename... RArgs> |
| 168 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Return(fptr)(CArgs...), RArgs &&... args) |
| 169 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 170 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 171 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 172 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 173 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 174 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 177 | // Returns a non-const variable copy of const v |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 178 | Ice::Variable *createUnconstCast(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Constant *v) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 179 | { |
| 180 | Ice::Variable *result = function->makeVariable(v->getType()); |
| 181 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Bitcast, result, v); |
| 182 | basicBlock->appendInst(cast); |
| 183 | return result; |
| 184 | } |
| 185 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 186 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 187 | { |
| 188 | Ice::Variable *to = function->makeVariable(toType); |
| 189 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 190 | basicBlock->appendInst(cast); |
| 191 | return to; |
| 192 | } |
| 193 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 194 | 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] | 195 | { |
| 196 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 197 | // absolute address. We circumvent this by casting to a non-const variable, and loading |
| 198 | // from that. |
| 199 | if(auto *cptr = llvm::dyn_cast<Ice::Constant>(ptr)) |
| 200 | { |
| 201 | ptr = sz::createUnconstCast(function, basicBlock, cptr); |
| 202 | } |
| 203 | |
| 204 | Ice::Variable *result = function->makeVariable(type); |
| 205 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 206 | basicBlock->appendInst(load); |
| 207 | |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 212 | } // namespace |
| 213 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 214 | namespace rr { |
| 215 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 216 | class CoroutineGenerator; |
| 217 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 218 | |
| 219 | namespace { |
| 220 | |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 221 | // Used to automatically invoke llvm_shutdown() when driver is unloaded |
| 222 | llvm::llvm_shutdown_obj llvmShutdownObj; |
| 223 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 224 | // Default configuration settings. Must be accessed under mutex lock. |
| 225 | std::mutex defaultConfigLock; |
| 226 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 227 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 228 | // This uses a static in a function to avoid the cost of a global static |
| 229 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 230 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 231 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 232 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 235 | Ice::GlobalContext *context = nullptr; |
| 236 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 237 | Ice::CfgNode *entryBlock = nullptr; |
| 238 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 239 | Ice::CfgNode *basicBlock = nullptr; |
| 240 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 241 | rr::ELFMemoryStreamer *routine = nullptr; |
| 242 | |
| 243 | std::mutex codegenMutex; |
| 244 | |
| 245 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 246 | Ice::Fdstream *out = nullptr; |
| 247 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 248 | // Coroutine globals |
| 249 | rr::Type *coroYieldType = nullptr; |
| 250 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 251 | marl::Scheduler &getOrCreateScheduler() |
| 252 | { |
| 253 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 254 | marl::Scheduler::Config cfg; |
| 255 | cfg.setWorkerThreadCount(8); |
| 256 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 257 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 258 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 259 | return *scheduler; |
| 260 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 261 | } // Anonymous namespace |
| 262 | |
| 263 | namespace { |
| 264 | |
| 265 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 266 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 267 | #endif |
| 268 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 269 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 270 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 271 | #endif |
| 272 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 273 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 274 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 275 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 276 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 277 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 278 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 279 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 280 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 281 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 282 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 283 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 284 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 287 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 288 | { |
| 289 | switch(memoryOrder) |
| 290 | { |
| 291 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 292 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 293 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 294 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 295 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 296 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
| 297 | } |
| 298 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 299 | } |
| 300 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 301 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 302 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 303 | public: |
| 304 | const static bool ARM; |
| 305 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 306 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 307 | private: |
| 308 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 309 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 310 | #if defined(__i386__) || defined(__x86_64__) |
| 311 | # if defined(_WIN32) |
| 312 | __cpuid(registers, info); |
| 313 | # else |
| 314 | __asm volatile("cpuid" |
| 315 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 316 | : "a"(info)); |
| 317 | # endif |
| 318 | #else |
| 319 | registers[0] = 0; |
| 320 | registers[1] = 0; |
| 321 | registers[2] = 0; |
| 322 | registers[3] = 0; |
| 323 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 326 | static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 327 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 328 | #if defined(__arm__) || defined(__aarch64__) |
| 329 | return true; |
| 330 | #elif defined(__i386__) || defined(__x86_64__) |
| 331 | return false; |
| 332 | #elif defined(__mips__) |
| 333 | return false; |
| 334 | #else |
| 335 | # error "Unknown architecture" |
| 336 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 337 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 338 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 339 | static bool detectSSE4_1() |
| 340 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 341 | #if defined(__i386__) || defined(__x86_64__) |
| 342 | int registers[4]; |
| 343 | cpuid(registers, 1); |
| 344 | return (registers[2] & 0x00080000) != 0; |
| 345 | #else |
| 346 | return false; |
| 347 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 348 | } |
| 349 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 350 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 351 | const bool CPUID::ARM = CPUID::detectARM(); |
| 352 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
| 353 | const bool emulateIntrinsics = false; |
| 354 | const bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 355 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 356 | constexpr bool subzeroDumpEnabled = false; |
| 357 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 358 | |
| 359 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 360 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 361 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 362 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 363 | |
| 364 | } // anonymous namespace |
| 365 | |
| 366 | namespace rr { |
| 367 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 368 | std::string BackendName() |
| 369 | { |
| 370 | return "Subzero"; |
| 371 | } |
| 372 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 373 | const Capabilities Caps = { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 374 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | enum EmulatedType |
| 378 | { |
| 379 | EmulatedShift = 16, |
| 380 | EmulatedV2 = 2 << EmulatedShift, |
| 381 | EmulatedV4 = 4 << EmulatedShift, |
| 382 | EmulatedV8 = 8 << EmulatedShift, |
| 383 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 384 | |
| 385 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 386 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 387 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 388 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 389 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 390 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 391 | }; |
| 392 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 393 | class Value : public Ice::Operand |
| 394 | {}; |
| 395 | class SwitchCases : public Ice::InstSwitch |
| 396 | {}; |
| 397 | class BasicBlock : public Ice::CfgNode |
| 398 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 399 | |
| 400 | Ice::Type T(Type *t) |
| 401 | { |
| 402 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 403 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 406 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 407 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 408 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | Type *T(EmulatedType t) |
| 412 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 413 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 416 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 417 | { |
| 418 | std::vector<Ice::Type> result; |
| 419 | result.reserve(types.size()); |
| 420 | for(auto &t : types) |
| 421 | { |
| 422 | result.push_back(T(t)); |
| 423 | } |
| 424 | return result; |
| 425 | } |
| 426 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 427 | Value *V(Ice::Operand *v) |
| 428 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 429 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 432 | Ice::Operand *V(Value *v) |
| 433 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 434 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 437 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 438 | { |
| 439 | std::vector<Ice::Operand *> result; |
| 440 | result.reserve(values.size()); |
| 441 | for(auto &v : values) |
| 442 | { |
| 443 | result.push_back(V(v)); |
| 444 | } |
| 445 | return result; |
| 446 | } |
| 447 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 448 | BasicBlock *B(Ice::CfgNode *b) |
| 449 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 450 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static size_t typeSize(Type *type) |
| 454 | { |
| 455 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 456 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 457 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 458 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 459 | case Type_v2i32: return 8; |
| 460 | case Type_v4i16: return 8; |
| 461 | case Type_v2i16: return 4; |
| 462 | case Type_v8i8: return 8; |
| 463 | case Type_v4i8: return 4; |
| 464 | case Type_v2f32: return 8; |
| 465 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | return Ice::typeWidthInBytes(T(type)); |
| 470 | } |
| 471 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 472 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 473 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 474 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 475 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 476 | { |
| 477 | Nucleus::createRetVoid(); |
| 478 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 479 | |
| 480 | // Connect the entry block to the top of the initial basic block |
| 481 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 482 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 483 | } |
| 484 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 485 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 486 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 487 | |
| 488 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 489 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 490 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 494 | { |
| 495 | return §ionHeader(elfHeader)[index]; |
| 496 | } |
| 497 | |
| 498 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 499 | { |
| 500 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 501 | |
| 502 | uint32_t index = relocation.getSymbol(); |
| 503 | int table = relocationTable.sh_link; |
| 504 | void *symbolValue = nullptr; |
| 505 | |
| 506 | if(index != SHN_UNDEF) |
| 507 | { |
| 508 | if(table == SHN_UNDEF) return nullptr; |
| 509 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 510 | |
| 511 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 512 | if(index >= symtab_entries) |
| 513 | { |
| 514 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 515 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 518 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 519 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 521 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 522 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 523 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 524 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 525 | 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] | 526 | } |
| 527 | else |
| 528 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 529 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 530 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 533 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 534 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 535 | |
| 536 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 537 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 538 | switch(relocation.getType()) |
| 539 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 540 | case R_ARM_NONE: |
| 541 | // No relocation |
| 542 | break; |
| 543 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 544 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 545 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 546 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 547 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 548 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 549 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 550 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 551 | { |
| 552 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 553 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 554 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 555 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 556 | default: |
| 557 | ASSERT(false && "Unsupported relocation type"); |
| 558 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 559 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 560 | } |
| 561 | else |
| 562 | { |
| 563 | switch(relocation.getType()) |
| 564 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 565 | case R_386_NONE: |
| 566 | // No relocation |
| 567 | break; |
| 568 | case R_386_32: |
| 569 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 570 | break; |
| 571 | case R_386_PC32: |
| 572 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 573 | break; |
| 574 | default: |
| 575 | ASSERT(false && "Unsupported relocation type"); |
| 576 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 577 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 580 | return symbolValue; |
| 581 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 582 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 583 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 584 | { |
| 585 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 586 | |
| 587 | uint32_t index = relocation.getSymbol(); |
| 588 | int table = relocationTable.sh_link; |
| 589 | void *symbolValue = nullptr; |
| 590 | |
| 591 | if(index != SHN_UNDEF) |
| 592 | { |
| 593 | if(table == SHN_UNDEF) return nullptr; |
| 594 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 595 | |
| 596 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 597 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 598 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 599 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 600 | return nullptr; |
| 601 | } |
| 602 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 603 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 604 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 605 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 606 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 607 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 608 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 609 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 610 | 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] | 611 | } |
| 612 | else |
| 613 | { |
| 614 | return nullptr; |
| 615 | } |
| 616 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 617 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 618 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 619 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 620 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 621 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 622 | switch(relocation.getType()) |
| 623 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 624 | case R_X86_64_NONE: |
| 625 | // No relocation |
| 626 | break; |
| 627 | case R_X86_64_64: |
| 628 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 629 | break; |
| 630 | case R_X86_64_PC32: |
| 631 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 632 | break; |
| 633 | case R_X86_64_32S: |
| 634 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 635 | break; |
| 636 | default: |
| 637 | ASSERT(false && "Unsupported relocation type"); |
| 638 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | return symbolValue; |
| 642 | } |
| 643 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 644 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 645 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 646 | const void *entry; |
| 647 | size_t codeSize = 0; |
| 648 | }; |
| 649 | |
| 650 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 651 | { |
| 652 | ASSERT(functionNames.size() > 0); |
| 653 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 654 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 655 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 656 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 657 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 658 | if(!elfHeader->checkMagic()) |
| 659 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 660 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 664 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 665 | #if defined(__i386__) |
| 666 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 667 | #elif defined(__x86_64__) |
| 668 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 669 | #elif defined(__arm__) |
| 670 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 671 | #elif defined(__aarch64__) |
| 672 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 673 | #elif defined(__mips__) |
| 674 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 675 | #else |
| 676 | # error "Unsupported platform" |
| 677 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 678 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 679 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 680 | |
| 681 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 682 | { |
| 683 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 684 | { |
| 685 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 686 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 687 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 688 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 689 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 690 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 691 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 692 | { |
| 693 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 694 | { |
| 695 | return j; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 700 | return static_cast<size_t>(-1); |
| 701 | }; |
| 702 | |
| 703 | size_t index = findSectionNameEntryIndex(); |
| 704 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 705 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 706 | } |
| 707 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 708 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 709 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 710 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 711 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 712 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 713 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 714 | 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] | 715 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 716 | } |
| 717 | } |
| 718 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 719 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 720 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 721 | |
| 722 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 723 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 724 | 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] | 725 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 730 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | template<typename T> |
| 734 | struct ExecutableAllocator |
| 735 | { |
| 736 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 737 | template<class U> |
| 738 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 739 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 740 | |
| 741 | using value_type = T; |
| 742 | using size_type = std::size_t; |
| 743 | |
| 744 | T *allocate(size_type n) |
| 745 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 746 | return (T *)allocateMemoryPages( |
| 747 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void deallocate(T *p, size_type n) |
| 751 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 752 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 753 | } |
| 754 | }; |
| 755 | |
| 756 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 757 | { |
| 758 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 759 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 760 | |
| 761 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 762 | ELFMemoryStreamer() |
| 763 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 764 | { |
| 765 | position = 0; |
| 766 | buffer.reserve(0x1000); |
| 767 | } |
| 768 | |
| 769 | ~ELFMemoryStreamer() override |
| 770 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void write8(uint8_t Value) override |
| 774 | { |
| 775 | if(position == (uint64_t)buffer.size()) |
| 776 | { |
| 777 | buffer.push_back(Value); |
| 778 | position++; |
| 779 | } |
| 780 | else if(position < (uint64_t)buffer.size()) |
| 781 | { |
| 782 | buffer[position] = Value; |
| 783 | position++; |
| 784 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 785 | else |
| 786 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void writeBytes(llvm::StringRef Bytes) override |
| 790 | { |
| 791 | std::size_t oldSize = buffer.size(); |
| 792 | buffer.resize(oldSize + Bytes.size()); |
| 793 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 794 | position += Bytes.size(); |
| 795 | } |
| 796 | |
| 797 | uint64_t tell() const override { return position; } |
| 798 | |
| 799 | void seek(uint64_t Off) override { position = Off; } |
| 800 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 801 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 802 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 803 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 804 | |
| 805 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 806 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 807 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 808 | for(auto &entryPoint : entryPoints) |
| 809 | { |
| 810 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 811 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 812 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 813 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 814 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 815 | } |
| 816 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 817 | void finalize() |
| 818 | { |
| 819 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 820 | |
| 821 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 822 | } |
| 823 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 824 | void setEntry(int index, const void *func) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 825 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 826 | ASSERT(func); |
| 827 | funcs[index] = func; |
| 828 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 829 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 830 | const void *getEntry(int index) const override |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 831 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 832 | ASSERT(funcs[index]); |
| 833 | return funcs[index]; |
| 834 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 835 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 836 | 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] | 837 | { |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 838 | // Check if we already have a suitable constant. |
| 839 | for(const auto &c : constantsPool) |
| 840 | { |
| 841 | void *ptr = c.data.get(); |
| 842 | size_t space = c.space; |
| 843 | |
| 844 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 845 | |
| 846 | if(space < size) |
| 847 | { |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | if(memcmp(data, alignedPtr, size) == 0) |
| 852 | { |
| 853 | return alignedPtr; |
| 854 | } |
| 855 | } |
| 856 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 857 | // TODO(b/148086935): Replace with a buffer allocator. |
| 858 | size_t space = size + alignment; |
| 859 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 860 | void *ptr = buf.get(); |
| 861 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 862 | ASSERT(alignedPtr); |
| 863 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 864 | constantsPool.emplace_back(std::move(buf), space); |
| 865 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 866 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 867 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 868 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 869 | private: |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 870 | struct Constant |
| 871 | { |
| 872 | Constant(std::unique_ptr<uint8_t[]> data, size_t space) |
| 873 | : data(std::move(data)) |
| 874 | , space(space) |
| 875 | {} |
| 876 | |
| 877 | std::unique_ptr<uint8_t[]> data; |
| 878 | size_t space; |
| 879 | }; |
| 880 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 881 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 882 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 883 | std::size_t position; |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 884 | std::vector<Constant> constantsPool; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 885 | }; |
| 886 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 887 | #ifdef ENABLE_RR_PRINT |
| 888 | void VPrintf(const std::vector<Value *> &vals) |
| 889 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 890 | 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] | 891 | } |
| 892 | #endif // ENABLE_RR_PRINT |
| 893 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 894 | Nucleus::Nucleus() |
| 895 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 896 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 897 | |
| 898 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 899 | Ice::ClFlags::getParsedClFlags(Flags); |
| 900 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 901 | #if defined(__arm__) |
| 902 | Flags.setTargetArch(Ice::Target_ARM32); |
| 903 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 904 | #elif defined(__mips__) |
| 905 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 906 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 907 | #else // x86 |
| 908 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 909 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 910 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 911 | Flags.setOutFileType(Ice::FT_Elf); |
| 912 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
| 913 | Flags.setApplicationBinaryInterface(Ice::ABI_Platform); |
| 914 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 915 | Flags.setDisableHybridAssembly(true); |
| 916 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 917 | // Emit functions into separate sections in the ELF so we can find them by name |
| 918 | Flags.setFunctionSections(true); |
| 919 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 920 | static llvm::raw_os_ostream cout(std::cout); |
| 921 | static llvm::raw_os_ostream cerr(std::cerr); |
| 922 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 923 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 924 | { |
| 925 | // Decorate text asm with liveness info |
| 926 | Flags.setDecorateAsm(true); |
| 927 | } |
| 928 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 929 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 930 | { |
| 931 | std::error_code errorCode; |
| 932 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 933 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 934 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 935 | } |
| 936 | else |
| 937 | { |
| 938 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 939 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 940 | ::routine = elfMemory; |
| 941 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 942 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 943 | #if !__has_feature(memory_sanitizer) |
| 944 | // thread_local variables in shared libraries are initialized at load-time, |
| 945 | // but this is not observed by MemorySanitizer if the loader itself was not |
| 946 | // instrumented, leading to false-positive unitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 947 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 948 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 949 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | Nucleus::~Nucleus() |
| 953 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 954 | delete Variable::unmaterializedVariables; |
| 955 | Variable::unmaterializedVariables = nullptr; |
| 956 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 957 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 958 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 959 | |
| 960 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 961 | ::allocator = nullptr; |
| 962 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 963 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 964 | ::function = nullptr; |
| 965 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 966 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 967 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 968 | |
| 969 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 970 | ::elfFile = nullptr; |
| 971 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 972 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 973 | ::out = nullptr; |
| 974 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 975 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 976 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 977 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 978 | |
| 979 | ::codegenMutex.unlock(); |
| 980 | } |
| 981 | |
| 982 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 983 | { |
| 984 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 985 | ::defaultConfig() = cfg; |
| 986 | } |
| 987 | |
| 988 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 989 | { |
| 990 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 991 | auto &config = ::defaultConfig(); |
| 992 | config = cfgEdit.apply(config); |
| 993 | } |
| 994 | |
| 995 | Config Nucleus::getDefaultConfig() |
| 996 | { |
| 997 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 998 | return ::defaultConfig(); |
| 999 | } |
| 1000 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1001 | // This function lowers and produces executable binary code in memory for the input functions, |
| 1002 | // and returns a Routine with the entry points to these functions. |
| 1003 | template<size_t Count> |
| 1004 | 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] | 1005 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1006 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 1007 | // and GlobalContext::emitItems. |
| 1008 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1009 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1010 | { |
| 1011 | // 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] | 1012 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | ::context->emitFileHeader(); |
| 1016 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1017 | // Translate |
| 1018 | |
| 1019 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1020 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1021 | Ice::Cfg *currFunc = functions[i]; |
| 1022 | |
| 1023 | // Install function allocator in TLS for Cfg-specific container allocators |
| 1024 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 1025 | |
| 1026 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 1027 | |
| 1028 | rr::optimize(currFunc); |
| 1029 | |
| 1030 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1031 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1032 | |
| 1033 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1034 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1035 | |
| 1036 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1037 | |
| 1038 | if(subzeroEmitTextAsm) |
| 1039 | { |
| 1040 | currFunc->emit(); |
| 1041 | } |
| 1042 | |
| 1043 | currFunc->emitIAS(); |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame] | 1044 | |
| 1045 | if(currFunc->hasError()) |
| 1046 | { |
| 1047 | return nullptr; |
| 1048 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1049 | } |
| 1050 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1051 | // Emit items |
| 1052 | |
| 1053 | ::context->lowerGlobals(""); |
| 1054 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1055 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1056 | |
| 1057 | for(size_t i = 0; i < Count; ++i) |
| 1058 | { |
| 1059 | Ice::Cfg *currFunc = functions[i]; |
| 1060 | |
| 1061 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1062 | auto globals = currFunc->getGlobalInits(); |
| 1063 | if(globals && !globals->empty()) |
| 1064 | { |
| 1065 | ::context->getGlobals()->merge(globals.get()); |
| 1066 | } |
| 1067 | |
| 1068 | auto assembler = currFunc->releaseAssembler(); |
| 1069 | assembler->alignFunction(); |
| 1070 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1071 | } |
| 1072 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1073 | ::context->lowerGlobals("last"); |
| 1074 | ::context->lowerConstants(); |
| 1075 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1076 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1077 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1078 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1079 | objectWriter->writeNonUserSections(); |
| 1080 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1081 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1082 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1083 | ASSERT(entryPoints.size() == Count); |
| 1084 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1085 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1086 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1090 | |
| 1091 | Routine *handoffRoutine = ::routine; |
| 1092 | ::routine = nullptr; |
| 1093 | |
| 1094 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1095 | } |
| 1096 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1097 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 1098 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1099 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1100 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1101 | } |
| 1102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1103 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1104 | { |
| 1105 | Ice::Type type = T(t); |
| 1106 | int typeSize = Ice::typeWidthInBytes(type); |
| 1107 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1108 | |
| 1109 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1110 | auto address = ::function->makeVariable(T(getPointerType(t))); |
| 1111 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); |
| 1112 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1113 | |
| 1114 | return V(address); |
| 1115 | } |
| 1116 | |
| 1117 | BasicBlock *Nucleus::createBasicBlock() |
| 1118 | { |
| 1119 | return B(::function->makeNode()); |
| 1120 | } |
| 1121 | |
| 1122 | BasicBlock *Nucleus::getInsertBlock() |
| 1123 | { |
| 1124 | return B(::basicBlock); |
| 1125 | } |
| 1126 | |
| 1127 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1128 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1129 | // 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] | 1130 | |
| 1131 | Variable::materializeAll(); |
| 1132 | |
| 1133 | ::basicBlock = basicBlock; |
| 1134 | } |
| 1135 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1136 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1137 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1138 | ASSERT(::function == nullptr); |
| 1139 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1140 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1141 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1142 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1143 | |
| 1144 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1145 | |
| 1146 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1147 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1148 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1149 | // 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] | 1150 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1151 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1152 | ::entryBlock = ::function->getEntryNode(); |
| 1153 | ::basicBlock = ::function->makeNode(); |
| 1154 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | Value *Nucleus::getArgument(unsigned int index) |
| 1158 | { |
| 1159 | return V(::function->getArgs()[index]); |
| 1160 | } |
| 1161 | |
| 1162 | void Nucleus::createRetVoid() |
| 1163 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1164 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1165 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1166 | // Code generated after this point is unreachable, so any variables |
| 1167 | // being read can safely return an undefined value. We have to avoid |
| 1168 | // materializing variables after the terminator ret instruction. |
| 1169 | Variable::killUnmaterialized(); |
| 1170 | |
| 1171 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1172 | ::basicBlock->appendInst(ret); |
| 1173 | } |
| 1174 | |
| 1175 | void Nucleus::createRet(Value *v) |
| 1176 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1177 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1178 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1179 | // Code generated after this point is unreachable, so any variables |
| 1180 | // being read can safely return an undefined value. We have to avoid |
| 1181 | // materializing variables after the terminator ret instruction. |
| 1182 | Variable::killUnmaterialized(); |
| 1183 | |
| 1184 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1185 | ::basicBlock->appendInst(ret); |
| 1186 | } |
| 1187 | |
| 1188 | void Nucleus::createBr(BasicBlock *dest) |
| 1189 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1190 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1191 | Variable::materializeAll(); |
| 1192 | |
| 1193 | auto br = Ice::InstBr::create(::function, dest); |
| 1194 | ::basicBlock->appendInst(br); |
| 1195 | } |
| 1196 | |
| 1197 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1198 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1199 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1200 | Variable::materializeAll(); |
| 1201 | |
| 1202 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1203 | ::basicBlock->appendInst(br); |
| 1204 | } |
| 1205 | |
| 1206 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1207 | { |
| 1208 | switch(op) |
| 1209 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1210 | case Ice::InstArithmetic::Add: |
| 1211 | case Ice::InstArithmetic::Fadd: |
| 1212 | case Ice::InstArithmetic::Mul: |
| 1213 | case Ice::InstArithmetic::Fmul: |
| 1214 | case Ice::InstArithmetic::And: |
| 1215 | case Ice::InstArithmetic::Or: |
| 1216 | case Ice::InstArithmetic::Xor: |
| 1217 | return true; |
| 1218 | default: |
| 1219 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1224 | { |
| 1225 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1226 | |
| 1227 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1228 | |
| 1229 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1230 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1231 | ::basicBlock->appendInst(arithmetic); |
| 1232 | |
| 1233 | return V(result); |
| 1234 | } |
| 1235 | |
| 1236 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1237 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1238 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1239 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
| 1240 | } |
| 1241 | |
| 1242 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1243 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1244 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1245 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
| 1246 | } |
| 1247 | |
| 1248 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1249 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1250 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1251 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
| 1252 | } |
| 1253 | |
| 1254 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1255 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1256 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1257 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
| 1258 | } |
| 1259 | |
| 1260 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1261 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1263 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
| 1264 | } |
| 1265 | |
| 1266 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1267 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1268 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1269 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
| 1270 | } |
| 1271 | |
| 1272 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1273 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1274 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1275 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
| 1276 | } |
| 1277 | |
| 1278 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1279 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1280 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1281 | return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs); |
| 1282 | } |
| 1283 | |
| 1284 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1285 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1286 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1287 | return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs); |
| 1288 | } |
| 1289 | |
| 1290 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1291 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1292 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1293 | return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs); |
| 1294 | } |
| 1295 | |
| 1296 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1297 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1299 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
| 1300 | } |
| 1301 | |
| 1302 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1305 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1306 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1307 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1308 | return nullptr; |
| 1309 | } |
| 1310 | |
| 1311 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1312 | { |
| 1313 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1317 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1318 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1319 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
| 1320 | } |
| 1321 | |
| 1322 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1323 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1324 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1325 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
| 1326 | } |
| 1327 | |
| 1328 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1329 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1330 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1331 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
| 1332 | } |
| 1333 | |
| 1334 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1335 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1337 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
| 1338 | } |
| 1339 | |
| 1340 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1343 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
| 1344 | } |
| 1345 | |
| 1346 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1347 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1348 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1349 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1350 | } |
| 1351 | |
| 1352 | Value *Nucleus::createNeg(Value *v) |
| 1353 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1355 | return createSub(createNullValue(T(v->getType())), v); |
| 1356 | } |
| 1357 | |
| 1358 | Value *Nucleus::createFNeg(Value *v) |
| 1359 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1361 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1362 | 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] | 1363 | |
| 1364 | return createFSub(negativeZero, v); |
| 1365 | } |
| 1366 | |
| 1367 | Value *Nucleus::createNot(Value *v) |
| 1368 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1369 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1370 | if(Ice::isScalarIntegerType(v->getType())) |
| 1371 | { |
| 1372 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1373 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1374 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1375 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1376 | 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] | 1377 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1378 | } |
| 1379 | } |
| 1380 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1381 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1382 | { |
| 1383 | #if defined(__i386__) || defined(__x86_64__) |
| 1384 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1385 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1386 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1387 | #else |
| 1388 | if(atomic) |
| 1389 | { |
| 1390 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1391 | } |
| 1392 | if(memoryOrder != std::memory_order_relaxed) |
| 1393 | { |
| 1394 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1395 | } |
| 1396 | #endif |
| 1397 | |
| 1398 | // Vulkan doesn't allow sequential memory order |
| 1399 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1400 | } |
| 1401 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1402 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1403 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1404 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1405 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1406 | |
| 1407 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1408 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1409 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1410 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1411 | { |
| 1412 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1413 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1414 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1415 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1417 | Int x = *Pointer<Int>(pointer); |
| 1418 | |
| 1419 | Int4 vector; |
| 1420 | vector = Insert(vector, x, 0); |
| 1421 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1422 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1423 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1424 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1425 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1426 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1427 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1428 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1429 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1430 | Int x = *Pointer<Int>(pointer); |
| 1431 | Int y = *Pointer<Int>(pointer + 4); |
| 1432 | |
| 1433 | Int4 vector; |
| 1434 | vector = Insert(vector, x, 0); |
| 1435 | vector = Insert(vector, y, 1); |
| 1436 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1437 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1438 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1439 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1440 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1441 | else |
| 1442 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1443 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1444 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1445 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1446 | 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] | 1447 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 33a77f7 | 2021-02-08 15:04:38 -0500 | [diff] [blame^] | 1448 | auto load = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1449 | load->addArg(ptr); |
| 1450 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1451 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1452 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1453 | } |
| 1454 | else |
| 1455 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1456 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1457 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1458 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1459 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1460 | return V(result); |
| 1461 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1462 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1463 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1464 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1465 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1466 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1467 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1468 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1469 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1470 | if(align != 0) |
| 1471 | { |
| 1472 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1473 | call->addArg(ptr); |
| 1474 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1475 | ::basicBlock->appendInst(call); |
| 1476 | } |
| 1477 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1478 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1479 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1480 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1481 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1482 | { |
| 1483 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1484 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1485 | if(typeSize(type) == 4) |
| 1486 | { |
| 1487 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1488 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1489 | ::basicBlock->appendInst(bitcast); |
| 1490 | |
| 1491 | RValue<Int4> v(V(vector)); |
| 1492 | |
| 1493 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1494 | Int x = Extract(v, 0); |
| 1495 | *Pointer<Int>(pointer) = x; |
| 1496 | } |
| 1497 | else if(typeSize(type) == 8) |
| 1498 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1499 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1500 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1501 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1502 | ::basicBlock->appendInst(bitcast); |
| 1503 | |
| 1504 | RValue<Int4> v(V(vector)); |
| 1505 | |
| 1506 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1507 | Int x = Extract(v, 0); |
| 1508 | *Pointer<Int>(pointer) = x; |
| 1509 | Int y = Extract(v, 1); |
| 1510 | *Pointer<Int>(pointer + 4) = y; |
| 1511 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1512 | else |
| 1513 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1514 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1515 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1516 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1517 | 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^] | 1518 | auto store = Ice::InstIntrinsic::create(::function, 3, nullptr, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1519 | store->addArg(value); |
| 1520 | store->addArg(ptr); |
| 1521 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1522 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1523 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1524 | } |
| 1525 | else |
| 1526 | { |
| 1527 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1528 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1529 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1530 | ::basicBlock->appendInst(store); |
| 1531 | } |
| 1532 | |
| 1533 | return value; |
| 1534 | } |
| 1535 | |
| 1536 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1537 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1538 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1539 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1540 | |
| 1541 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1542 | { |
| 1543 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1544 | |
| 1545 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1546 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1547 | return ptr; |
| 1548 | } |
| 1549 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1550 | return createAdd(ptr, createConstantInt(offset)); |
| 1551 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1552 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1553 | if(!Ice::isByteSizedType(T(type))) |
| 1554 | { |
| 1555 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1556 | } |
| 1557 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1558 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1559 | { |
| 1560 | if(unsignedIndex) |
| 1561 | { |
| 1562 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1563 | } |
| 1564 | else |
| 1565 | { |
| 1566 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | return createAdd(ptr, index); |
| 1571 | } |
| 1572 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1573 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1574 | { |
| 1575 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1576 | |
| 1577 | 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^] | 1578 | auto inst = Ice::InstIntrinsic::create(::function, 0, result, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1579 | auto op = ::context->getConstantInt32(rmwOp); |
| 1580 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1581 | inst->addArg(op); |
| 1582 | inst->addArg(ptr); |
| 1583 | inst->addArg(value); |
| 1584 | inst->addArg(order); |
| 1585 | ::basicBlock->appendInst(inst); |
| 1586 | |
| 1587 | return V(result); |
| 1588 | } |
| 1589 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1590 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1591 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1592 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1593 | return createAtomicRMW(Ice::Intrinsics::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1597 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1598 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1599 | return createAtomicRMW(Ice::Intrinsics::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1603 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1604 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1605 | return createAtomicRMW(Ice::Intrinsics::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1609 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1610 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1611 | return createAtomicRMW(Ice::Intrinsics::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1615 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1616 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1617 | return createAtomicRMW(Ice::Intrinsics::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1621 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1622 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1623 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1627 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1628 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1629 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1630 | |
| 1631 | 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^] | 1632 | auto inst = Ice::InstIntrinsic::create(::function, 0, result, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1633 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1634 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1635 | inst->addArg(ptr); |
| 1636 | inst->addArg(compare); |
| 1637 | inst->addArg(value); |
| 1638 | inst->addArg(orderEq); |
| 1639 | inst->addArg(orderNeq); |
| 1640 | ::basicBlock->appendInst(inst); |
| 1641 | |
| 1642 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1646 | { |
| 1647 | if(v->getType() == T(destType)) |
| 1648 | { |
| 1649 | return v; |
| 1650 | } |
| 1651 | |
| 1652 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1653 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1654 | ::basicBlock->appendInst(cast); |
| 1655 | |
| 1656 | return V(result); |
| 1657 | } |
| 1658 | |
| 1659 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1660 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1661 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1662 | return createCast(Ice::InstCast::Trunc, v, destType); |
| 1663 | } |
| 1664 | |
| 1665 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1666 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1667 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1668 | return createCast(Ice::InstCast::Zext, v, destType); |
| 1669 | } |
| 1670 | |
| 1671 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1672 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1673 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1674 | return createCast(Ice::InstCast::Sext, v, destType); |
| 1675 | } |
| 1676 | |
| 1677 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1678 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1679 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1680 | return createCast(Ice::InstCast::Fptoui, v, destType); |
| 1681 | } |
| 1682 | |
| 1683 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1684 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1685 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1686 | return createCast(Ice::InstCast::Fptosi, v, destType); |
| 1687 | } |
| 1688 | |
| 1689 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1690 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1691 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1692 | return createCast(Ice::InstCast::Sitofp, v, destType); |
| 1693 | } |
| 1694 | |
| 1695 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1696 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1697 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1698 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
| 1699 | } |
| 1700 | |
| 1701 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1702 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1703 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1704 | return createCast(Ice::InstCast::Fpext, v, destType); |
| 1705 | } |
| 1706 | |
| 1707 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1708 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1709 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1710 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1711 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1712 | // emulate them by writing to the stack and reading back as the destination type. |
| 1713 | if(emulateMismatchedBitCast) |
| 1714 | { |
| 1715 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1716 | { |
| 1717 | Value *address = allocateStackVariable(destType); |
| 1718 | createStore(v, address, T(v->getType())); |
| 1719 | return createLoad(address, destType); |
| 1720 | } |
| 1721 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1722 | { |
| 1723 | Value *address = allocateStackVariable(T(v->getType())); |
| 1724 | createStore(v, address, T(v->getType())); |
| 1725 | return createLoad(address, destType); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1730 | } |
| 1731 | |
| 1732 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1733 | { |
| 1734 | ASSERT(lhs->getType() == rhs->getType()); |
| 1735 | |
| 1736 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1737 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1738 | ::basicBlock->appendInst(cmp); |
| 1739 | |
| 1740 | return V(result); |
| 1741 | } |
| 1742 | |
| 1743 | Value *Nucleus::createPtrEQ(Value *lhs, Value *rhs) |
| 1744 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1745 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1746 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1747 | } |
| 1748 | |
| 1749 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1750 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1751 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1752 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1753 | } |
| 1754 | |
| 1755 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1756 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1757 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1758 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1759 | } |
| 1760 | |
| 1761 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1762 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1763 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1764 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1765 | } |
| 1766 | |
| 1767 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1768 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1769 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1770 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1771 | } |
| 1772 | |
| 1773 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1774 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1775 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1776 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1777 | } |
| 1778 | |
| 1779 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1780 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1781 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1782 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1783 | } |
| 1784 | |
| 1785 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1786 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1787 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1788 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1789 | } |
| 1790 | |
| 1791 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1792 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1793 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1794 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1795 | } |
| 1796 | |
| 1797 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1798 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1799 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1800 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1801 | } |
| 1802 | |
| 1803 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1804 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1805 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1806 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1807 | } |
| 1808 | |
| 1809 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1810 | { |
| 1811 | ASSERT(lhs->getType() == rhs->getType()); |
| 1812 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1813 | |
| 1814 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1815 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1816 | ::basicBlock->appendInst(cmp); |
| 1817 | |
| 1818 | return V(result); |
| 1819 | } |
| 1820 | |
| 1821 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1822 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1823 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1824 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
| 1825 | } |
| 1826 | |
| 1827 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1828 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1829 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1830 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
| 1831 | } |
| 1832 | |
| 1833 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1834 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1835 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1836 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
| 1837 | } |
| 1838 | |
| 1839 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1840 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1841 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1842 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
| 1843 | } |
| 1844 | |
| 1845 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1846 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1847 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1848 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
| 1849 | } |
| 1850 | |
| 1851 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1852 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1853 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1854 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
| 1855 | } |
| 1856 | |
| 1857 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1858 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1859 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1860 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
| 1861 | } |
| 1862 | |
| 1863 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1864 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1865 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1866 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
| 1867 | } |
| 1868 | |
| 1869 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1870 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1871 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1872 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
| 1873 | } |
| 1874 | |
| 1875 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1876 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1877 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1878 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
| 1879 | } |
| 1880 | |
| 1881 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1882 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1883 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1884 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
| 1885 | } |
| 1886 | |
| 1887 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1888 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1889 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1890 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
| 1891 | } |
| 1892 | |
| 1893 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1894 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1895 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1896 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
| 1897 | } |
| 1898 | |
| 1899 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1900 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1901 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1902 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1903 | } |
| 1904 | |
| 1905 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1906 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1907 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1908 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1909 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1910 | ::basicBlock->appendInst(extract); |
| 1911 | |
| 1912 | return V(result); |
| 1913 | } |
| 1914 | |
| 1915 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1916 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1917 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1918 | auto result = ::function->makeVariable(vector->getType()); |
| 1919 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1920 | ::basicBlock->appendInst(insert); |
| 1921 | |
| 1922 | return V(result); |
| 1923 | } |
| 1924 | |
| 1925 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 1926 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1927 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1928 | ASSERT(V1->getType() == V2->getType()); |
| 1929 | |
| 1930 | int size = Ice::typeNumElements(V1->getType()); |
| 1931 | auto result = ::function->makeVariable(V1->getType()); |
| 1932 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1933 | |
| 1934 | for(int i = 0; i < size; i++) |
| 1935 | { |
| 1936 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1937 | } |
| 1938 | |
| 1939 | ::basicBlock->appendInst(shuffle); |
| 1940 | |
| 1941 | return V(result); |
| 1942 | } |
| 1943 | |
| 1944 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1945 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1946 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1947 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1948 | |
| 1949 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1950 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1951 | ::basicBlock->appendInst(select); |
| 1952 | |
| 1953 | return V(result); |
| 1954 | } |
| 1955 | |
| 1956 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1957 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1958 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1959 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1960 | ::basicBlock->appendInst(switchInst); |
| 1961 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1962 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1966 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1967 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1968 | switchCases->addBranch(label, label, branch); |
| 1969 | } |
| 1970 | |
| 1971 | void Nucleus::createUnreachable() |
| 1972 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1973 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1974 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1975 | ::basicBlock->appendInst(unreachable); |
| 1976 | } |
| 1977 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1978 | Type *Nucleus::getType(Value *value) |
| 1979 | { |
| 1980 | return T(V(value)->getType()); |
| 1981 | } |
| 1982 | |
| 1983 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1984 | { |
| 1985 | Ice::Type vecTy = T(vectorType); |
| 1986 | switch(vecTy) |
| 1987 | { |
| 1988 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1989 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1990 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1991 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1992 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1993 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1994 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1995 | default: |
| 1996 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 1997 | return {}; |
| 1998 | } |
| 1999 | } |
| 2000 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2001 | Type *Nucleus::getPointerType(Type *ElementType) |
| 2002 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 2003 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2004 | } |
| 2005 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2006 | static constexpr Ice::Type getNaturalIntType() |
| 2007 | { |
| 2008 | constexpr size_t intSize = sizeof(int); |
| 2009 | static_assert(intSize == 4 || intSize == 8, ""); |
| 2010 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 2011 | } |
| 2012 | |
| 2013 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 2014 | { |
| 2015 | Ice::Type valueTy = T(valueType); |
| 2016 | switch(valueTy) |
| 2017 | { |
| 2018 | case Ice::IceType_i32: |
| 2019 | return T(getNaturalIntType()); |
| 2020 | |
| 2021 | case Ice::IceType_f32: |
| 2022 | return T(Ice::IceType_f64); |
| 2023 | |
| 2024 | default: |
| 2025 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 2026 | return {}; |
| 2027 | } |
| 2028 | } |
| 2029 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2030 | Value *Nucleus::createNullValue(Type *Ty) |
| 2031 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2032 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2033 | if(Ice::isVectorType(T(Ty))) |
| 2034 | { |
| 2035 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2036 | 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] | 2037 | return createConstantVector(c, Ty); |
| 2038 | } |
| 2039 | else |
| 2040 | { |
| 2041 | return V(::context->getConstantZero(T(Ty))); |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | Value *Nucleus::createConstantLong(int64_t i) |
| 2046 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2047 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2048 | return V(::context->getConstantInt64(i)); |
| 2049 | } |
| 2050 | |
| 2051 | Value *Nucleus::createConstantInt(int i) |
| 2052 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2053 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2054 | return V(::context->getConstantInt32(i)); |
| 2055 | } |
| 2056 | |
| 2057 | Value *Nucleus::createConstantInt(unsigned int i) |
| 2058 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2059 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2060 | return V(::context->getConstantInt32(i)); |
| 2061 | } |
| 2062 | |
| 2063 | Value *Nucleus::createConstantBool(bool b) |
| 2064 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2065 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2066 | return V(::context->getConstantInt1(b)); |
| 2067 | } |
| 2068 | |
| 2069 | Value *Nucleus::createConstantByte(signed char i) |
| 2070 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2071 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2072 | return V(::context->getConstantInt8(i)); |
| 2073 | } |
| 2074 | |
| 2075 | Value *Nucleus::createConstantByte(unsigned char i) |
| 2076 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2077 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2078 | return V(::context->getConstantInt8(i)); |
| 2079 | } |
| 2080 | |
| 2081 | Value *Nucleus::createConstantShort(short i) |
| 2082 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2083 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2084 | return V(::context->getConstantInt16(i)); |
| 2085 | } |
| 2086 | |
| 2087 | Value *Nucleus::createConstantShort(unsigned short i) |
| 2088 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2089 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2090 | return V(::context->getConstantInt16(i)); |
| 2091 | } |
| 2092 | |
| 2093 | Value *Nucleus::createConstantFloat(float x) |
| 2094 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2095 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2096 | return V(::context->getConstantFloat(x)); |
| 2097 | } |
| 2098 | |
| 2099 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2100 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2101 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2102 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2103 | } |
| 2104 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2105 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2106 | { |
| 2107 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2108 | } |
| 2109 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2110 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2111 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2112 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2113 | const int vectorSize = 16; |
| 2114 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2115 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2116 | |
| 2117 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2118 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2119 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2120 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2121 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2122 | |
| 2123 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2124 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2125 | case Ice::IceType_v4i32: |
| 2126 | case Ice::IceType_v4i1: |
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 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] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2133 | case Ice::IceType_v4f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2134 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2135 | 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] | 2136 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2137 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2138 | } |
| 2139 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2140 | case Ice::IceType_v8i16: |
| 2141 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2142 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2143 | 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] | 2144 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2145 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2146 | } |
| 2147 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2148 | case Ice::IceType_v16i8: |
| 2149 | case Ice::IceType_v16i1: |
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 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] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2156 | case Type_v2i32: |
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 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] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2163 | case Type_v2f32: |
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 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] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2170 | case Type_v4i16: |
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 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] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2177 | case Type_v8i8: |
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[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] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2184 | case Type_v4i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2185 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2186 | 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] | 2187 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2188 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2189 | } |
| 2190 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2191 | default: |
| 2192 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2193 | } |
| 2194 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2195 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2196 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2197 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2198 | return V(result); |
| 2199 | } |
| 2200 | |
| 2201 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2202 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2203 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2204 | } |
| 2205 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2206 | Value *Nucleus::createConstantString(const char *v) |
| 2207 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2208 | // 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] | 2209 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2210 | } |
| 2211 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2212 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2213 | { |
| 2214 | return T(Ice::IceType_void); |
| 2215 | } |
| 2216 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2217 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2218 | { |
| 2219 | return T(Ice::IceType_i1); |
| 2220 | } |
| 2221 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2222 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2223 | { |
| 2224 | return T(Ice::IceType_i8); |
| 2225 | } |
| 2226 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2227 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2228 | { |
| 2229 | return T(Ice::IceType_i8); |
| 2230 | } |
| 2231 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2232 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2233 | { |
| 2234 | return T(Ice::IceType_i16); |
| 2235 | } |
| 2236 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2237 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2238 | { |
| 2239 | return T(Ice::IceType_i16); |
| 2240 | } |
| 2241 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2242 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2243 | { |
| 2244 | return T(Type_v4i8); |
| 2245 | } |
| 2246 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2247 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2248 | { |
| 2249 | return T(Type_v4i8); |
| 2250 | } |
| 2251 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2252 | namespace { |
| 2253 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2254 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2255 | 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] | 2256 | } |
| 2257 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2258 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2259 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2260 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2264 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2265 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } // namespace |
| 2268 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2269 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2270 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2271 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2272 | if(emulateIntrinsics) |
| 2273 | { |
| 2274 | Byte8 result; |
| 2275 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2276 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2277 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2278 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2279 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2280 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2281 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2282 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2283 | |
| 2284 | return result; |
| 2285 | } |
| 2286 | else |
| 2287 | { |
| 2288 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2289 | 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^] | 2290 | auto paddusb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2291 | paddusb->addArg(x.value()); |
| 2292 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2293 | ::basicBlock->appendInst(paddusb); |
| 2294 | |
| 2295 | return RValue<Byte8>(V(result)); |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2300 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2301 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2302 | if(emulateIntrinsics) |
| 2303 | { |
| 2304 | Byte8 result; |
| 2305 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2306 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2307 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2308 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2309 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2310 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2311 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2312 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2313 | |
| 2314 | return result; |
| 2315 | } |
| 2316 | else |
| 2317 | { |
| 2318 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2319 | 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^] | 2320 | auto psubusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2321 | psubusw->addArg(x.value()); |
| 2322 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2323 | ::basicBlock->appendInst(psubusw); |
| 2324 | |
| 2325 | return RValue<Byte8>(V(result)); |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 2330 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2331 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2332 | return RValue<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2336 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2337 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2338 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2342 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2343 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2344 | if(emulateIntrinsics) |
| 2345 | { |
| 2346 | SByte8 result; |
| 2347 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2348 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2349 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2350 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2351 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2352 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2353 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2354 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2355 | |
| 2356 | return result; |
| 2357 | } |
| 2358 | else |
| 2359 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2360 | #if defined(__i386__) || defined(__x86_64__) |
| 2361 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2362 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2363 | 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] | 2364 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2365 | return As<SByte8>(hi | lo); |
| 2366 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2367 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2368 | #endif |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2371 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2372 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2373 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2374 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2375 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2376 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2377 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2378 | 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] | 2379 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2380 | else |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2381 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2382 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2383 | 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^] | 2384 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2385 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2386 | ::basicBlock->appendInst(movmsk); |
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 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2389 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2390 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2391 | |
| 2392 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2393 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2394 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2395 | // } |
| 2396 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2397 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2398 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2399 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2400 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2401 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2402 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2403 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2404 | { |
| 2405 | return T(Type_v8i8); |
| 2406 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2407 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2408 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2409 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2410 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2411 | // } |
| 2412 | |
| 2413 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2414 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2415 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2416 | // } |
| 2417 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2418 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 2419 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2421 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2422 | } |
| 2423 | |
| 2424 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2427 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2428 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2429 | SByte8 result; |
| 2430 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2431 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2432 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2433 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2434 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2435 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2436 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2437 | 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] | 2438 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2439 | return result; |
| 2440 | } |
| 2441 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2442 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2443 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2444 | 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^] | 2445 | auto paddsb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2446 | paddsb->addArg(x.value()); |
| 2447 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2448 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2449 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2450 | return RValue<SByte8>(V(result)); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2453 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2454 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2455 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2457 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2458 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2459 | SByte8 result; |
| 2460 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2461 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2462 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2463 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2464 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2465 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2466 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2467 | 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] | 2468 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2469 | return result; |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2472 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2473 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2474 | 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^] | 2475 | auto psubsb = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2476 | psubsb->addArg(x.value()); |
| 2477 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2478 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2479 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2480 | return RValue<SByte8>(V(result)); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2483 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2484 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2485 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2486 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2487 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2488 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2489 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2490 | 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] | 2491 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2492 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2493 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2494 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2495 | 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^] | 2496 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2497 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2498 | ::basicBlock->appendInst(movmsk); |
| 2499 | |
| 2500 | return RValue<Int>(V(result)) & 0xFF; |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2503 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2504 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 2505 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2506 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2507 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2508 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2509 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2510 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2511 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2512 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2513 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2514 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2515 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2516 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2517 | { |
| 2518 | return T(Type_v8i8); |
| 2519 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2520 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2521 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2522 | { |
| 2523 | return T(Ice::IceType_v16i8); |
| 2524 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2525 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2526 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2527 | { |
| 2528 | return T(Ice::IceType_v16i8); |
| 2529 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2530 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2531 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2532 | { |
| 2533 | return T(Type_v2i16); |
| 2534 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2535 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2536 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2537 | { |
| 2538 | return T(Type_v2i16); |
| 2539 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2540 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2541 | Short4::Short4(RValue<Int4> cast) |
| 2542 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2543 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2544 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2545 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2546 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2547 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2548 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2549 | |
| 2550 | storeValue(short4); |
| 2551 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2552 | |
| 2553 | // Short4::Short4(RValue<Float> cast) |
| 2554 | // { |
| 2555 | // } |
| 2556 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2557 | Short4::Short4(RValue<Float4> cast) |
| 2558 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2559 | // TODO(b/150791192): Generalize and optimize |
| 2560 | auto smin = std::numeric_limits<short>::min(); |
| 2561 | auto smax = std::numeric_limits<short>::max(); |
| 2562 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2566 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2567 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2568 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2569 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2570 | Short4 result; |
| 2571 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2572 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2573 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2574 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2575 | |
| 2576 | return result; |
| 2577 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2578 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2579 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2580 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2581 | } |
| 2582 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2583 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2584 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2585 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2586 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2587 | if(emulateIntrinsics) |
| 2588 | { |
| 2589 | Short4 result; |
| 2590 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2591 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2592 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2593 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2594 | |
| 2595 | return result; |
| 2596 | } |
| 2597 | else |
| 2598 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2599 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2604 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2605 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2606 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2607 | 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] | 2608 | ::basicBlock->appendInst(cmp); |
| 2609 | |
| 2610 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2611 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2612 | ::basicBlock->appendInst(select); |
| 2613 | |
| 2614 | return RValue<Short4>(V(result)); |
| 2615 | } |
| 2616 | |
| 2617 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2618 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2619 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2620 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2621 | 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] | 2622 | ::basicBlock->appendInst(cmp); |
| 2623 | |
| 2624 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2625 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2626 | ::basicBlock->appendInst(select); |
| 2627 | |
| 2628 | return RValue<Short4>(V(result)); |
| 2629 | } |
| 2630 | |
| 2631 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 2632 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2633 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2634 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2635 | } |
| 2636 | |
| 2637 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2638 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2639 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2640 | if(emulateIntrinsics) |
| 2641 | { |
| 2642 | Short4 result; |
| 2643 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2644 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2645 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2646 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2647 | |
| 2648 | return result; |
| 2649 | } |
| 2650 | else |
| 2651 | { |
| 2652 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2653 | 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^] | 2654 | auto paddsw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2655 | paddsw->addArg(x.value()); |
| 2656 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2657 | ::basicBlock->appendInst(paddsw); |
| 2658 | |
| 2659 | return RValue<Short4>(V(result)); |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2664 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2665 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2666 | if(emulateIntrinsics) |
| 2667 | { |
| 2668 | Short4 result; |
| 2669 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2670 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2671 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2672 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2673 | |
| 2674 | return result; |
| 2675 | } |
| 2676 | else |
| 2677 | { |
| 2678 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2679 | 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^] | 2680 | auto psubsw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2681 | psubsw->addArg(x.value()); |
| 2682 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2683 | ::basicBlock->appendInst(psubsw); |
| 2684 | |
| 2685 | return RValue<Short4>(V(result)); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2690 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2691 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2692 | if(emulateIntrinsics) |
| 2693 | { |
| 2694 | Short4 result; |
| 2695 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2696 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2697 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2698 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2699 | |
| 2700 | return result; |
| 2701 | } |
| 2702 | else |
| 2703 | { |
| 2704 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2705 | 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^] | 2706 | auto pmulhw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2707 | pmulhw->addArg(x.value()); |
| 2708 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2709 | ::basicBlock->appendInst(pmulhw); |
| 2710 | |
| 2711 | return RValue<Short4>(V(result)); |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2716 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2717 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2718 | if(emulateIntrinsics) |
| 2719 | { |
| 2720 | Int2 result; |
| 2721 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2722 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2723 | |
| 2724 | return result; |
| 2725 | } |
| 2726 | else |
| 2727 | { |
| 2728 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2729 | 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^] | 2730 | auto pmaddwd = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2731 | pmaddwd->addArg(x.value()); |
| 2732 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2733 | ::basicBlock->appendInst(pmaddwd); |
| 2734 | |
| 2735 | return As<Int2>(V(result)); |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2740 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2741 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2742 | if(emulateIntrinsics) |
| 2743 | { |
| 2744 | SByte8 result; |
| 2745 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2746 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2747 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2748 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2749 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2750 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2751 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2752 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2753 | |
| 2754 | return result; |
| 2755 | } |
| 2756 | else |
| 2757 | { |
| 2758 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2759 | 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^] | 2760 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2761 | pack->addArg(x.value()); |
| 2762 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2763 | ::basicBlock->appendInst(pack); |
| 2764 | |
| 2765 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2770 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2771 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2772 | if(emulateIntrinsics) |
| 2773 | { |
| 2774 | Byte8 result; |
| 2775 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2776 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2777 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2778 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2779 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2780 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2781 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2782 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2783 | |
| 2784 | return result; |
| 2785 | } |
| 2786 | else |
| 2787 | { |
| 2788 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2789 | 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^] | 2790 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2791 | pack->addArg(x.value()); |
| 2792 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2793 | ::basicBlock->appendInst(pack); |
| 2794 | |
| 2795 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2800 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2801 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2802 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2806 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2807 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2808 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2809 | } |
| 2810 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2811 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2812 | { |
| 2813 | return T(Type_v4i16); |
| 2814 | } |
| 2815 | |
| 2816 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2817 | { |
| 2818 | if(saturate) |
| 2819 | { |
| 2820 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2821 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2822 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2823 | // PackUnsigned takes care of 0x0000 saturation. |
| 2824 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2825 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2826 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2827 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2828 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2829 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2830 | Int4 int4(cast); |
| 2831 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2832 | } |
| 2833 | else |
| 2834 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2835 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2836 | } |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2839 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2840 | *this = Short4(Int4(cast)); |
| 2841 | } |
| 2842 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2843 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2844 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2845 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2846 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2850 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2851 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2855 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2856 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2857 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2858 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2859 | { |
| 2860 | UShort4 result; |
| 2861 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2862 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2863 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2864 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2865 | |
| 2866 | return result; |
| 2867 | } |
| 2868 | else |
| 2869 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2870 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2875 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2876 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2877 | if(emulateIntrinsics) |
| 2878 | { |
| 2879 | UShort4 result; |
| 2880 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2881 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2882 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2883 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2884 | |
| 2885 | return result; |
| 2886 | } |
| 2887 | else |
| 2888 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2889 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2894 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2895 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2896 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2897 | 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] | 2898 | ::basicBlock->appendInst(cmp); |
| 2899 | |
| 2900 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2901 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2902 | ::basicBlock->appendInst(select); |
| 2903 | |
| 2904 | return RValue<UShort4>(V(result)); |
| 2905 | } |
| 2906 | |
| 2907 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2908 | { |
| 2909 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2910 | 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] | 2911 | ::basicBlock->appendInst(cmp); |
| 2912 | |
| 2913 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2914 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2915 | ::basicBlock->appendInst(select); |
| 2916 | |
| 2917 | return RValue<UShort4>(V(result)); |
| 2918 | } |
| 2919 | |
| 2920 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 2921 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2922 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2923 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2924 | } |
| 2925 | |
| 2926 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2927 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2928 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2929 | if(emulateIntrinsics) |
| 2930 | { |
| 2931 | UShort4 result; |
| 2932 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2933 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2934 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2935 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2936 | |
| 2937 | return result; |
| 2938 | } |
| 2939 | else |
| 2940 | { |
| 2941 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2942 | 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^] | 2943 | auto paddusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2944 | paddusw->addArg(x.value()); |
| 2945 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2946 | ::basicBlock->appendInst(paddusw); |
| 2947 | |
| 2948 | return RValue<UShort4>(V(result)); |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2953 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2954 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2955 | if(emulateIntrinsics) |
| 2956 | { |
| 2957 | UShort4 result; |
| 2958 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2959 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2960 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2961 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2962 | |
| 2963 | return result; |
| 2964 | } |
| 2965 | else |
| 2966 | { |
| 2967 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2968 | 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^] | 2969 | auto psubusw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2970 | psubusw->addArg(x.value()); |
| 2971 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2972 | ::basicBlock->appendInst(psubusw); |
| 2973 | |
| 2974 | return RValue<UShort4>(V(result)); |
| 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2979 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2980 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2981 | if(emulateIntrinsics) |
| 2982 | { |
| 2983 | UShort4 result; |
| 2984 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2985 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2986 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2987 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 2988 | |
| 2989 | return result; |
| 2990 | } |
| 2991 | else |
| 2992 | { |
| 2993 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2994 | 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^] | 2995 | auto pmulhuw = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2996 | pmulhuw->addArg(x.value()); |
| 2997 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2998 | ::basicBlock->appendInst(pmulhuw); |
| 2999 | |
| 3000 | return RValue<UShort4>(V(result)); |
| 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3005 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3006 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3007 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3008 | |
| 3009 | // Scalarized implementation. |
| 3010 | Int4 result; |
| 3011 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 3012 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 3013 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 3014 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 3015 | |
| 3016 | return result; |
| 3017 | } |
| 3018 | |
| 3019 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3020 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3021 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3022 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3023 | |
| 3024 | if(false) // Partial product based implementation. |
| 3025 | { |
| 3026 | auto xh = x >> 16; |
| 3027 | auto yh = y >> 16; |
| 3028 | auto xl = x & UInt4(0x0000FFFF); |
| 3029 | auto yl = y & UInt4(0x0000FFFF); |
| 3030 | auto xlyh = xl * yh; |
| 3031 | auto xhyl = xh * yl; |
| 3032 | auto xlyhh = xlyh >> 16; |
| 3033 | auto xhylh = xhyl >> 16; |
| 3034 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3035 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3036 | auto xlylh = (xl * yl) >> 16; |
| 3037 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3038 | |
| 3039 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3040 | } |
| 3041 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3042 | // Scalarized implementation. |
| 3043 | Int4 result; |
| 3044 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3045 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3046 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3047 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3048 | |
| 3049 | return As<UInt4>(result); |
| 3050 | } |
| 3051 | |
| 3052 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3053 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3054 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3055 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3056 | return UShort4(0); |
| 3057 | } |
| 3058 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3059 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3060 | { |
| 3061 | return T(Type_v4i16); |
| 3062 | } |
| 3063 | |
| 3064 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 3065 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3066 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3067 | return RValue<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3071 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3072 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3073 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3077 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3078 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3079 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3080 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3081 | Short8 result; |
| 3082 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3083 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3084 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3085 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3086 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3087 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3088 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3089 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3090 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3091 | return result; |
| 3092 | } |
| 3093 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3094 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3095 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3098 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3099 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 3100 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3101 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3102 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3103 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3104 | Short8 result; |
| 3105 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 3106 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 3107 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 3108 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 3109 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 3110 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 3111 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 3112 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3113 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3114 | return result; |
| 3115 | } |
| 3116 | else |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3117 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3118 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3121 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3122 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3123 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3124 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3125 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3126 | return Int4(0); |
| 3127 | } |
| 3128 | |
| 3129 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 3130 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3131 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3132 | UNIMPLEMENTED_NO_BUG("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3133 | return Short8(0); |
| 3134 | } |
| 3135 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3136 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3137 | { |
| 3138 | return T(Ice::IceType_v8i16); |
| 3139 | } |
| 3140 | |
| 3141 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 3142 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3143 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3144 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3148 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3149 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3150 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3154 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3155 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3156 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3157 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3158 | UShort8 result; |
| 3159 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3160 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3161 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3162 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3163 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3164 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3165 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3166 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3167 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3168 | return result; |
| 3169 | } |
| 3170 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3171 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3172 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3175 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3176 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 3177 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3178 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3179 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3180 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3181 | UShort8 result; |
| 3182 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 3183 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 3184 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 3185 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 3186 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 3187 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 3188 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 3189 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3190 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3191 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3192 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3193 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3194 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3195 | return RValue<UShort8>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3198 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3199 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3200 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3201 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3202 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3203 | return UShort8(0); |
| 3204 | } |
| 3205 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3206 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3207 | { |
| 3208 | return T(Ice::IceType_v8i16); |
| 3209 | } |
| 3210 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3211 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3212 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3213 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3214 | RValue<Int> res = val; |
| 3215 | val += 1; |
| 3216 | return res; |
| 3217 | } |
| 3218 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3219 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3220 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3221 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3222 | val += 1; |
| 3223 | return val; |
| 3224 | } |
| 3225 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3226 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3227 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3228 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3229 | RValue<Int> res = val; |
| 3230 | val -= 1; |
| 3231 | return res; |
| 3232 | } |
| 3233 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3234 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3235 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3236 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3237 | val -= 1; |
| 3238 | return val; |
| 3239 | } |
| 3240 | |
| 3241 | RValue<Int> RoundInt(RValue<Float> cast) |
| 3242 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3243 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3244 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3245 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3246 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3247 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
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 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3250 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3251 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3252 | 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^] | 3253 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3254 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3255 | ::basicBlock->appendInst(nearbyint); |
| 3256 | |
| 3257 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3258 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3259 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3260 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3261 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3262 | { |
| 3263 | return T(Ice::IceType_i32); |
| 3264 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3265 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3266 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3267 | { |
| 3268 | return T(Ice::IceType_i64); |
| 3269 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3270 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3271 | UInt::UInt(RValue<Float> cast) |
| 3272 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3273 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3274 | // Smallest positive value representable in UInt, but not in Int |
| 3275 | const unsigned int ustart = 0x80000000u; |
| 3276 | const float ustartf = float(ustart); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3277 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3278 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3279 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3280 | // Check if the value can be represented as an Int |
| 3281 | IfThenElse(cast >= ustartf, |
| 3282 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3283 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3284 | // Otherwise, just convert normally |
| 3285 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3286 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3287 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3288 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3289 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3290 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3291 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3292 | RValue<UInt> res = val; |
| 3293 | val += 1; |
| 3294 | return res; |
| 3295 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3296 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3297 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3298 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3299 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3300 | val += 1; |
| 3301 | return val; |
| 3302 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3303 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3304 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3305 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3306 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3307 | RValue<UInt> res = val; |
| 3308 | val -= 1; |
| 3309 | return res; |
| 3310 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3311 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3312 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3313 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3314 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3315 | val -= 1; |
| 3316 | return val; |
| 3317 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3318 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3319 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3320 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3321 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3322 | // } |
| 3323 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3324 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3325 | { |
| 3326 | return T(Ice::IceType_i32); |
| 3327 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3328 | |
| 3329 | // Int2::Int2(RValue<Int> cast) |
| 3330 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3331 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3332 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3333 | // |
| 3334 | // Constant *shuffle[2]; |
| 3335 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3336 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3337 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3338 | // 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] | 3339 | // |
| 3340 | // storeValue(replicate); |
| 3341 | // } |
| 3342 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3343 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3344 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3345 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3346 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3347 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3348 | Int2 result; |
| 3349 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3350 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3351 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3352 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3353 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3354 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3355 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3356 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3359 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3360 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3361 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3362 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3363 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3364 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3365 | Int2 result; |
| 3366 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3367 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3368 | |
| 3369 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3370 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3371 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3372 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3373 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3374 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3375 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3376 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3377 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3378 | { |
| 3379 | return T(Type_v2i32); |
| 3380 | } |
| 3381 | |
| 3382 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 3383 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3384 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3385 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3386 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3387 | UInt2 result; |
| 3388 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3389 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3390 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3391 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3392 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3393 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3394 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3395 | return RValue<UInt2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3398 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3399 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3400 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3401 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3402 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3403 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3404 | UInt2 result; |
| 3405 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3406 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3407 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3408 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3409 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3410 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3411 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3412 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3413 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3414 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3415 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3416 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3417 | { |
| 3418 | return T(Type_v2i32); |
| 3419 | } |
| 3420 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3421 | Int4::Int4(RValue<Byte4> cast) |
| 3422 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3423 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3424 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3425 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3426 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3427 | |
| 3428 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3429 | 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] | 3430 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3431 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3432 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3433 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3434 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3435 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3436 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3437 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3438 | storeValue(f); |
| 3439 | } |
| 3440 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3441 | Int4::Int4(RValue<SByte4> cast) |
| 3442 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3445 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3446 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3447 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3448 | 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] | 3449 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3450 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3451 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3452 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3453 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3454 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3455 | |
| 3456 | *this = As<Int4>(e) >> 24; |
| 3457 | } |
| 3458 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3459 | Int4::Int4(RValue<Short4> cast) |
| 3460 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3461 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3463 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3464 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3465 | |
| 3466 | *this = As<Int4>(c) >> 16; |
| 3467 | } |
| 3468 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3469 | Int4::Int4(RValue<UShort4> cast) |
| 3470 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3471 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3472 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3473 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3474 | 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] | 3475 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3476 | storeValue(d); |
| 3477 | } |
| 3478 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3479 | Int4::Int4(RValue<Int> rhs) |
| 3480 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3481 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3482 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3483 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3484 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3485 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3486 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3487 | |
| 3488 | storeValue(replicate); |
| 3489 | } |
| 3490 | |
| 3491 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3492 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3493 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3494 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3495 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3496 | Int4 result; |
| 3497 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3498 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3499 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3500 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3501 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3502 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3503 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3504 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3505 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3506 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3509 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3510 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3511 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3512 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3513 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3514 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3515 | Int4 result; |
| 3516 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3517 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3518 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3519 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3520 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3521 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3522 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3523 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3524 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3525 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3528 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3529 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3530 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3531 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3532 | return RValue<Int4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3536 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3537 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3538 | return RValue<Int4>(Nucleus::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3542 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3543 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3544 | return RValue<Int4>(Nucleus::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3548 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3549 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3550 | return RValue<Int4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3554 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3555 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3556 | return RValue<Int4>(Nucleus::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3560 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3561 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3562 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3566 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3567 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3568 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3569 | 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] | 3570 | ::basicBlock->appendInst(cmp); |
| 3571 | |
| 3572 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3573 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3574 | ::basicBlock->appendInst(select); |
| 3575 | |
| 3576 | return RValue<Int4>(V(result)); |
| 3577 | } |
| 3578 | |
| 3579 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3580 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3581 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3582 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3583 | 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] | 3584 | ::basicBlock->appendInst(cmp); |
| 3585 | |
| 3586 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3587 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3588 | ::basicBlock->appendInst(select); |
| 3589 | |
| 3590 | return RValue<Int4>(V(result)); |
| 3591 | } |
| 3592 | |
| 3593 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3594 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3595 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3596 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3597 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3598 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3599 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3600 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3601 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3602 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3603 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3604 | 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^] | 3605 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3606 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3607 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3608 | |
| 3609 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3610 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3611 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3612 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3613 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3614 | { |
| 3615 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3616 | |
| 3617 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3618 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3619 | // saturate to 0x80000000. |
| 3620 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3621 | |
| 3622 | if(emulateIntrinsics || CPUID::ARM) |
| 3623 | { |
| 3624 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3625 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3626 | } |
| 3627 | else |
| 3628 | { |
| 3629 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3630 | 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^] | 3631 | auto nearbyint = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3632 | nearbyint->addArg(clamped.value()); |
| 3633 | ::basicBlock->appendInst(nearbyint); |
| 3634 | |
| 3635 | return RValue<Int4>(V(result)); |
| 3636 | } |
| 3637 | } |
| 3638 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3639 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3640 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3641 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3642 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3643 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3644 | Short8 result; |
| 3645 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3646 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3647 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3648 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3649 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3650 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3651 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3652 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3653 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3654 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3655 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3656 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3657 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3658 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3659 | 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^] | 3660 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3661 | pack->addArg(x.value()); |
| 3662 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3663 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3664 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3665 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3666 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3667 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3668 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3669 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3670 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3671 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3672 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
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<Int4> sx = As<Int4>(x); |
| 3675 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3676 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3677 | RValue<Int4> sy = As<Int4>(y); |
| 3678 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3679 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3680 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3681 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3682 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3683 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3684 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3685 | 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^] | 3686 | auto pack = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3687 | pack->addArg(x.value()); |
| 3688 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3689 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3690 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3691 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3692 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3693 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3694 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3695 | RValue<Int> SignMask(RValue<Int4> x) |
| 3696 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3697 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3698 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3699 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3700 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3701 | 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] | 3702 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3703 | else |
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 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3706 | 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^] | 3707 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3708 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3709 | ::basicBlock->appendInst(movmsk); |
| 3710 | |
| 3711 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3712 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3713 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3714 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3715 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3716 | { |
| 3717 | return T(Ice::IceType_v4i32); |
| 3718 | } |
| 3719 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3720 | UInt4::UInt4(RValue<Float4> cast) |
| 3721 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3722 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3723 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3724 | // Smallest positive value representable in UInt, but not in Int |
| 3725 | const unsigned int ustart = 0x80000000u; |
| 3726 | const float ustartf = float(ustart); |
| 3727 | |
| 3728 | // Check if the value can be represented as an Int |
| 3729 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3730 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3731 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3732 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3733 | (~uiValue & Int4(cast)); |
| 3734 | // 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] | 3735 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3736 | } |
| 3737 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3738 | UInt4::UInt4(RValue<UInt> rhs) |
| 3739 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3740 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3741 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3742 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3743 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3744 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3745 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3746 | |
| 3747 | storeValue(replicate); |
| 3748 | } |
| 3749 | |
| 3750 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3751 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3752 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3753 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3754 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3755 | UInt4 result; |
| 3756 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3757 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3758 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3759 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3760 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3761 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3762 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3763 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3764 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3765 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3766 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3767 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3768 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3769 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3770 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3771 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3772 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3773 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3774 | UInt4 result; |
| 3775 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3776 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3777 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3778 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3779 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3780 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3781 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3782 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3783 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3784 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3785 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3786 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3787 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3788 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3789 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3790 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3791 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3795 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3796 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3797 | return RValue<UInt4>(Nucleus::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3801 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3802 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3803 | return RValue<UInt4>(Nucleus::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3807 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3808 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3809 | return RValue<UInt4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3813 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3814 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3815 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3819 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3820 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3821 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3825 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3826 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3827 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3828 | 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] | 3829 | ::basicBlock->appendInst(cmp); |
| 3830 | |
| 3831 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3832 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3833 | ::basicBlock->appendInst(select); |
| 3834 | |
| 3835 | return RValue<UInt4>(V(result)); |
| 3836 | } |
| 3837 | |
| 3838 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3839 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3840 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3841 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3842 | 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] | 3843 | ::basicBlock->appendInst(cmp); |
| 3844 | |
| 3845 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3846 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3847 | ::basicBlock->appendInst(select); |
| 3848 | |
| 3849 | return RValue<UInt4>(V(result)); |
| 3850 | } |
| 3851 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3852 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3853 | { |
| 3854 | return T(Ice::IceType_v4i32); |
| 3855 | } |
| 3856 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3857 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3858 | { |
| 3859 | return T(Ice::IceType_i16); |
| 3860 | } |
| 3861 | |
| 3862 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3863 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3864 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3865 | return 1.0f / x; |
| 3866 | } |
| 3867 | |
| 3868 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3869 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3870 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3871 | return Rcp_pp(Sqrt(x)); |
| 3872 | } |
| 3873 | |
| 3874 | RValue<Float> Sqrt(RValue<Float> x) |
| 3875 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3876 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3877 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3878 | 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^] | 3879 | auto sqrt = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3880 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3881 | ::basicBlock->appendInst(sqrt); |
| 3882 | |
| 3883 | return RValue<Float>(V(result)); |
| 3884 | } |
| 3885 | |
| 3886 | RValue<Float> Round(RValue<Float> x) |
| 3887 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3888 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3889 | return Float4(Round(Float4(x))).x; |
| 3890 | } |
| 3891 | |
| 3892 | RValue<Float> Trunc(RValue<Float> x) |
| 3893 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3894 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3895 | return Float4(Trunc(Float4(x))).x; |
| 3896 | } |
| 3897 | |
| 3898 | RValue<Float> Frac(RValue<Float> x) |
| 3899 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3900 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3901 | return Float4(Frac(Float4(x))).x; |
| 3902 | } |
| 3903 | |
| 3904 | RValue<Float> Floor(RValue<Float> x) |
| 3905 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3906 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3907 | return Float4(Floor(Float4(x))).x; |
| 3908 | } |
| 3909 | |
| 3910 | RValue<Float> Ceil(RValue<Float> x) |
| 3911 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3912 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3913 | return Float4(Ceil(Float4(x))).x; |
| 3914 | } |
| 3915 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3916 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3917 | { |
| 3918 | return T(Ice::IceType_f32); |
| 3919 | } |
| 3920 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3921 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3922 | { |
| 3923 | return T(Type_v2f32); |
| 3924 | } |
| 3925 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3926 | Float4::Float4(RValue<Float> rhs) |
| 3927 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3928 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3929 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3930 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3931 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3932 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3933 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3934 | |
| 3935 | storeValue(replicate); |
| 3936 | } |
| 3937 | |
| 3938 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3939 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3940 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3941 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3942 | 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] | 3943 | ::basicBlock->appendInst(cmp); |
| 3944 | |
| 3945 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3946 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3947 | ::basicBlock->appendInst(select); |
| 3948 | |
| 3949 | return RValue<Float4>(V(result)); |
| 3950 | } |
| 3951 | |
| 3952 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3953 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3954 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3955 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3956 | 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] | 3957 | ::basicBlock->appendInst(cmp); |
| 3958 | |
| 3959 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3960 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3961 | ::basicBlock->appendInst(select); |
| 3962 | |
| 3963 | return RValue<Float4>(V(result)); |
| 3964 | } |
| 3965 | |
| 3966 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3967 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3968 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3969 | return Float4(1.0f) / x; |
| 3970 | } |
| 3971 | |
| 3972 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3973 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3974 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3975 | return Rcp_pp(Sqrt(x)); |
| 3976 | } |
| 3977 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 3978 | bool HasRcpApprox() |
| 3979 | { |
| 3980 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3981 | return false; |
| 3982 | } |
| 3983 | |
| 3984 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 3985 | { |
| 3986 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3987 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 3988 | return { 0.0f }; |
| 3989 | } |
| 3990 | |
| 3991 | RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2) |
| 3992 | { |
| 3993 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3994 | UNREACHABLE("RValue<Float> RcpApprox()"); |
| 3995 | return { 0.0f }; |
| 3996 | } |
| 3997 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 3998 | bool HasRcpSqrtApprox() |
| 3999 | { |
| 4000 | return false; |
| 4001 | } |
| 4002 | |
| 4003 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 4004 | { |
| 4005 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4006 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 4007 | return { 0.0f }; |
| 4008 | } |
| 4009 | |
| 4010 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 4011 | { |
| 4012 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4013 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 4014 | return { 0.0f }; |
| 4015 | } |
| 4016 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4017 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4018 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4019 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4020 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4021 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4022 | Float4 result; |
| 4023 | result.x = Sqrt(Float(Float4(x).x)); |
| 4024 | result.y = Sqrt(Float(Float4(x).y)); |
| 4025 | result.z = Sqrt(Float(Float4(x).z)); |
| 4026 | result.w = Sqrt(Float(Float4(x).w)); |
| 4027 | |
| 4028 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4029 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4030 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4031 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4032 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4033 | 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^] | 4034 | auto sqrt = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4035 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4036 | ::basicBlock->appendInst(sqrt); |
| 4037 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4038 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4039 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4040 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4041 | |
| 4042 | RValue<Int> SignMask(RValue<Float4> x) |
| 4043 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4044 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4045 | if(emulateIntrinsics || CPUID::ARM) |
| 4046 | { |
| 4047 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4048 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4049 | } |
| 4050 | else |
| 4051 | { |
| 4052 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4053 | 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^] | 4054 | auto movmsk = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4055 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4056 | ::basicBlock->appendInst(movmsk); |
| 4057 | |
| 4058 | return RValue<Int>(V(result)); |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 4063 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4064 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4065 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 4069 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4071 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 4075 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4076 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4077 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4081 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4082 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4083 | return RValue<Int4>(Nucleus::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 4087 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4088 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4089 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 4093 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4094 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4095 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 4099 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4100 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4101 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 4105 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4106 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4107 | return RValue<Int4>(Nucleus::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 4111 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4112 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4113 | return RValue<Int4>(Nucleus::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4117 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4118 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4119 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4123 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4124 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4125 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4129 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4130 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4131 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | RValue<Float4> Round(RValue<Float4> x) |
| 4135 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4136 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4137 | if(emulateIntrinsics || CPUID::ARM) |
| 4138 | { |
| 4139 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4140 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4141 | } |
| 4142 | else if(CPUID::SSE4_1) |
| 4143 | { |
| 4144 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4145 | 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^] | 4146 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4147 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4148 | round->addArg(::context->getConstantInt32(0)); |
| 4149 | ::basicBlock->appendInst(round); |
| 4150 | |
| 4151 | return RValue<Float4>(V(result)); |
| 4152 | } |
| 4153 | else |
| 4154 | { |
| 4155 | return Float4(RoundInt(x)); |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | RValue<Float4> Trunc(RValue<Float4> x) |
| 4160 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4161 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4162 | if(CPUID::SSE4_1) |
| 4163 | { |
| 4164 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4165 | 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^] | 4166 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4167 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4168 | round->addArg(::context->getConstantInt32(3)); |
| 4169 | ::basicBlock->appendInst(round); |
| 4170 | |
| 4171 | return RValue<Float4>(V(result)); |
| 4172 | } |
| 4173 | else |
| 4174 | { |
| 4175 | return Float4(Int4(x)); |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | RValue<Float4> Frac(RValue<Float4> x) |
| 4180 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4181 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4182 | Float4 frc; |
| 4183 | |
| 4184 | if(CPUID::SSE4_1) |
| 4185 | { |
| 4186 | frc = x - Floor(x); |
| 4187 | } |
| 4188 | else |
| 4189 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4190 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4191 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4192 | 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] | 4193 | } |
| 4194 | |
| 4195 | // x - floor(x) can be 1.0 for very small negative x. |
| 4196 | // Clamp against the value just below 1.0. |
| 4197 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4198 | } |
| 4199 | |
| 4200 | RValue<Float4> Floor(RValue<Float4> x) |
| 4201 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4202 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4203 | if(CPUID::SSE4_1) |
| 4204 | { |
| 4205 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4206 | 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^] | 4207 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4208 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4209 | round->addArg(::context->getConstantInt32(1)); |
| 4210 | ::basicBlock->appendInst(round); |
| 4211 | |
| 4212 | return RValue<Float4>(V(result)); |
| 4213 | } |
| 4214 | else |
| 4215 | { |
| 4216 | return x - Frac(x); |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4221 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4222 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4223 | if(CPUID::SSE4_1) |
| 4224 | { |
| 4225 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4226 | 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^] | 4227 | auto round = Ice::InstIntrinsic::create(::function, 2, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4228 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4229 | round->addArg(::context->getConstantInt32(2)); |
| 4230 | ::basicBlock->appendInst(round); |
| 4231 | |
| 4232 | return RValue<Float4>(V(result)); |
| 4233 | } |
| 4234 | else |
| 4235 | { |
| 4236 | return -Floor(-x); |
| 4237 | } |
| 4238 | } |
| 4239 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4240 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4241 | { |
| 4242 | return T(Ice::IceType_v4f32); |
| 4243 | } |
| 4244 | |
| 4245 | RValue<Long> Ticks() |
| 4246 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4247 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4248 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4249 | return Long(Int(0)); |
| 4250 | } |
| 4251 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4252 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4253 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4254 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4255 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4256 | } |
| 4257 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4258 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4259 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4260 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4261 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4262 | } |
| 4263 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4264 | 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] | 4265 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4266 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4267 | 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] | 4268 | } |
| 4269 | |
| 4270 | void Breakpoint() |
| 4271 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4272 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4273 | 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^] | 4274 | auto trap = Ice::InstIntrinsic::create(::function, 0, nullptr, intrinsic); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4275 | ::basicBlock->appendInst(trap); |
| 4276 | } |
| 4277 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4278 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4279 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4280 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4281 | 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^] | 4282 | auto inst = Ice::InstIntrinsic::create(::function, 0, nullptr, intrinsic); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4283 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4284 | inst->addArg(order); |
| 4285 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4286 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4287 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4288 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4289 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4290 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4291 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4292 | return nullptr; |
| 4293 | } |
| 4294 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4295 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4296 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4297 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4298 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4299 | |
| 4300 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4301 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4302 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4303 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4304 | } |
| 4305 | |
| 4306 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4307 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4308 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4309 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4310 | } |
| 4311 | |
| 4312 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4313 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4314 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4315 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4316 | } |
| 4317 | |
| 4318 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4319 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4320 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4321 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4322 | } |
| 4323 | |
| 4324 | RValue<Float> Exp2(RValue<Float> x) |
| 4325 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4326 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4327 | return emulated::Exp2(x); |
| 4328 | } |
| 4329 | |
| 4330 | RValue<Float> Log2(RValue<Float> x) |
| 4331 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4332 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4333 | return emulated::Log2(x); |
| 4334 | } |
| 4335 | |
| 4336 | RValue<Float4> Sin(RValue<Float4> x) |
| 4337 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4338 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4339 | return optimal::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | RValue<Float4> Cos(RValue<Float4> x) |
| 4343 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4344 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4345 | return optimal::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | RValue<Float4> Tan(RValue<Float4> x) |
| 4349 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4350 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4351 | return optimal::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4352 | } |
| 4353 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4354 | RValue<Float4> Asin(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4355 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4356 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4357 | if(p == Precision::Full) |
| 4358 | { |
| 4359 | return emulated::Asin(x); |
| 4360 | } |
| 4361 | return optimal::Asin_8_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4362 | } |
| 4363 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4364 | RValue<Float4> Acos(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4367 | // Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial approximation |
| 4368 | // version of acos, unlike for Asin, which requires higher precision algorithms. |
| 4369 | return optimal::Acos_4_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | RValue<Float4> Atan(RValue<Float4> x) |
| 4373 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4374 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4375 | return optimal::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4379 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4380 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4381 | return optimal::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | RValue<Float4> Cosh(RValue<Float4> x) |
| 4385 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4386 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4387 | return optimal::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | RValue<Float4> Tanh(RValue<Float4> x) |
| 4391 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4392 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4393 | return optimal::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | RValue<Float4> Asinh(RValue<Float4> x) |
| 4397 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4398 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4399 | return optimal::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | RValue<Float4> Acosh(RValue<Float4> x) |
| 4403 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4404 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4405 | return optimal::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4406 | } |
| 4407 | |
| 4408 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4409 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4410 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4411 | return optimal::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 4415 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4416 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4417 | return optimal::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4421 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4422 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4423 | return optimal::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | RValue<Float4> Exp(RValue<Float4> x) |
| 4427 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4428 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4429 | return optimal::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | RValue<Float4> Log(RValue<Float4> x) |
| 4433 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4434 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4435 | return optimal::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4439 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4440 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4441 | return optimal::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | RValue<Float4> Log2(RValue<Float4> x) |
| 4445 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4446 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4447 | return optimal::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4451 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4452 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4453 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4454 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4455 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4456 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4457 | } |
| 4458 | else |
| 4459 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4460 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4461 | 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^] | 4462 | auto ctlz = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4463 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4464 | ::basicBlock->appendInst(ctlz); |
| 4465 | |
| 4466 | return RValue<UInt>(V(result)); |
| 4467 | } |
| 4468 | } |
| 4469 | |
| 4470 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4471 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4472 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4473 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4474 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4475 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4476 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4477 | } |
| 4478 | else |
| 4479 | { |
| 4480 | // TODO: implement vectorized version in Subzero |
| 4481 | UInt4 result; |
| 4482 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4483 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4484 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4485 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4486 | return result; |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4491 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4492 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4493 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4494 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4495 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4496 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4497 | } |
| 4498 | else |
| 4499 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4500 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4501 | 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^] | 4502 | auto ctlz = Ice::InstIntrinsic::create(::function, 1, result, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4503 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4504 | ::basicBlock->appendInst(ctlz); |
| 4505 | |
| 4506 | return RValue<UInt>(V(result)); |
| 4507 | } |
| 4508 | } |
| 4509 | |
| 4510 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4511 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4512 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4513 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4514 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4515 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4516 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4517 | } |
| 4518 | else |
| 4519 | { |
| 4520 | // TODO: implement vectorized version in Subzero |
| 4521 | UInt4 result; |
| 4522 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4523 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4524 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4525 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4526 | return result; |
| 4527 | } |
| 4528 | } |
| 4529 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4530 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4531 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4532 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4533 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4534 | } |
| 4535 | |
| 4536 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4537 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4538 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4539 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4540 | } |
| 4541 | |
| 4542 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4543 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4544 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4545 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4546 | } |
| 4547 | |
| 4548 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4549 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4550 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4551 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4552 | } |
| 4553 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4554 | void EmitDebugLocation() |
| 4555 | { |
| 4556 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4557 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4558 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4559 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4560 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4561 | void FlushDebug() {} |
| 4562 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4563 | namespace { |
| 4564 | namespace coro { |
| 4565 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4566 | // Instance data per generated coroutine |
| 4567 | // This is the "handle" type used for Coroutine functions |
| 4568 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4569 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4570 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4571 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4572 | bool done = false; // the coroutine should stop at the next yield() |
| 4573 | bool terminated = false; // the coroutine has finished. |
| 4574 | bool inRoutine = false; // is the coroutine currently executing? |
| 4575 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4576 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4577 | void *promisePtr = nullptr; |
| 4578 | }; |
| 4579 | |
| 4580 | CoroutineData *createCoroutineData() |
| 4581 | { |
| 4582 | return new CoroutineData{}; |
| 4583 | } |
| 4584 | |
| 4585 | void destroyCoroutineData(CoroutineData *coroData) |
| 4586 | { |
| 4587 | delete coroData; |
| 4588 | } |
| 4589 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4590 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4591 | // caller's call to await(). |
| 4592 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4593 | // is called. |
| 4594 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4595 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4596 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4597 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4598 | ASSERT(coroData->inRoutine); |
| 4599 | coroData->inRoutine = false; |
| 4600 | coroData->mainFiber->notify(); |
| 4601 | while(!coroData->inRoutine) |
| 4602 | { |
| 4603 | coroData->routineFiber->wait(); |
| 4604 | } |
| 4605 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4606 | } |
| 4607 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4608 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4609 | // or the coroutine terminates. |
| 4610 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4611 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4612 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4613 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4614 | ASSERT(!coroData->inRoutine); |
| 4615 | coroData->inRoutine = true; |
| 4616 | coroData->routineFiber->notify(); |
| 4617 | while(coroData->inRoutine) |
| 4618 | { |
| 4619 | coroData->mainFiber->wait(); |
| 4620 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4621 | } |
| 4622 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4623 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4624 | // until the coroutine ends, and deletes the coroutine data. |
| 4625 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4626 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4627 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4628 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4629 | ASSERT(!coroData->inRoutine); |
| 4630 | if(!coroData->terminated) |
| 4631 | { |
| 4632 | coroData->done = true; |
| 4633 | coroData->inRoutine = true; |
| 4634 | coroData->routineFiber->notify(); |
| 4635 | while(!coroData->terminated) |
| 4636 | { |
| 4637 | coroData->mainFiber->wait(); |
| 4638 | } |
| 4639 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4640 | if(coroData->useInternalScheduler) |
| 4641 | { |
| 4642 | ::getOrCreateScheduler().unbind(); |
| 4643 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4644 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | namespace detail { |
| 4648 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4649 | } // namespace detail |
| 4650 | |
| 4651 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4652 | { |
| 4653 | ASSERT(!detail::coroHandle); |
| 4654 | detail::coroHandle = handle; |
| 4655 | } |
| 4656 | |
| 4657 | Nucleus::CoroutineHandle getHandleParam() |
| 4658 | { |
| 4659 | ASSERT(detail::coroHandle); |
| 4660 | auto handle = detail::coroHandle; |
| 4661 | detail::coroHandle = {}; |
| 4662 | return handle; |
| 4663 | } |
| 4664 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4665 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4666 | { |
| 4667 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4668 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4672 | { |
| 4673 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4674 | coroData->promisePtr = promisePtr; |
| 4675 | } |
| 4676 | |
| 4677 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4678 | { |
| 4679 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4680 | return coroData->promisePtr; |
| 4681 | } |
| 4682 | |
| 4683 | } // namespace coro |
| 4684 | } // namespace |
| 4685 | |
| 4686 | // Used to generate coroutines. |
| 4687 | // Lifetime: from yield to acquireCoroutine |
| 4688 | class CoroutineGenerator |
| 4689 | { |
| 4690 | public: |
| 4691 | CoroutineGenerator() |
| 4692 | { |
| 4693 | } |
| 4694 | |
| 4695 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4696 | void generateCoroutineBegin() |
| 4697 | { |
| 4698 | // Begin building the main coroutine_begin() function. |
| 4699 | // We insert these instructions at the top of the entry node, |
| 4700 | // before existing reactor-generated instructions. |
| 4701 | |
| 4702 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4703 | // { |
| 4704 | // this->handle = coro::getHandleParam(); |
| 4705 | // |
| 4706 | // YieldType promise; |
| 4707 | // coro::setPromisePtr(handle, &promise); // For await |
| 4708 | // |
| 4709 | // ... <REACTOR CODE> ... |
| 4710 | // |
| 4711 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4712 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4713 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4714 | |
| 4715 | // YieldType promise; |
| 4716 | // coro::setPromisePtr(handle, &promise); // For await |
| 4717 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4718 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4722 | void generateYield(Value *val) |
| 4723 | { |
| 4724 | // ... <REACTOR CODE> ... |
| 4725 | // |
| 4726 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4727 | // if (!coro::suspend(handle)) { |
| 4728 | // return false; // coroutine has been stopped by the caller. |
| 4729 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4730 | // |
| 4731 | // ... <REACTOR CODE> ... |
| 4732 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4733 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4734 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4735 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4736 | // if (!coro::suspend(handle)) { |
| 4737 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4738 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4739 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4740 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4741 | |
| 4742 | // return false; // coroutine has been stopped by the caller. |
| 4743 | ::basicBlock = doneBlock; |
| 4744 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4745 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4746 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4747 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4751 | |
| 4752 | // Generates the await function for the current coroutine. |
| 4753 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4754 | static FunctionUniquePtr generateAwaitFunction() |
| 4755 | { |
| 4756 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4757 | // { |
| 4758 | // if (coro::isDone()) |
| 4759 | // { |
| 4760 | // return false; |
| 4761 | // } |
| 4762 | // else // resume |
| 4763 | // { |
| 4764 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4765 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4766 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4767 | // return true; |
| 4768 | // } |
| 4769 | // } |
| 4770 | |
| 4771 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4772 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4773 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4774 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4775 | |
| 4776 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4777 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4778 | |
| 4779 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4780 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4781 | |
| 4782 | auto doneBlock = awaitFunc->makeNode(); |
| 4783 | { |
| 4784 | // return false; |
| 4785 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4786 | doneBlock->appendInst(ret); |
| 4787 | } |
| 4788 | |
| 4789 | auto resumeBlock = awaitFunc->makeNode(); |
| 4790 | { |
| 4791 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4792 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4793 | |
| 4794 | // *out = *promise; |
| 4795 | // Load promise value |
| 4796 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4797 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4798 | resumeBlock->appendInst(load); |
| 4799 | // Then store it in output param |
| 4800 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4801 | resumeBlock->appendInst(store); |
| 4802 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4803 | // coro::resume(handle); |
| 4804 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4805 | |
| 4806 | // return true; |
| 4807 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4808 | resumeBlock->appendInst(ret); |
| 4809 | } |
| 4810 | |
| 4811 | // if (coro::isDone()) |
| 4812 | // { |
| 4813 | // <doneBlock> |
| 4814 | // } |
| 4815 | // else // resume |
| 4816 | // { |
| 4817 | // <resumeBlock> |
| 4818 | // } |
| 4819 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4820 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4821 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4822 | bb->appendInst(br); |
| 4823 | |
| 4824 | return FunctionUniquePtr{ awaitFunc }; |
| 4825 | } |
| 4826 | |
| 4827 | // Generates the destroy function for the current coroutine. |
| 4828 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4829 | static FunctionUniquePtr generateDestroyFunction() |
| 4830 | { |
| 4831 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4832 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4833 | // 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] | 4834 | // return; |
| 4835 | // } |
| 4836 | |
| 4837 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4838 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4839 | |
| 4840 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4841 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4842 | |
| 4843 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4844 | |
| 4845 | auto *bb = destroyFunc->getEntryNode(); |
| 4846 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4847 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4848 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4849 | |
| 4850 | // return; |
| 4851 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4852 | bb->appendInst(ret); |
| 4853 | |
| 4854 | return FunctionUniquePtr{ destroyFunc }; |
| 4855 | } |
| 4856 | |
| 4857 | private: |
| 4858 | Ice::Variable *handle{}; |
| 4859 | Ice::Variable *promise{}; |
| 4860 | }; |
| 4861 | |
| 4862 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4863 | { |
| 4864 | // This doubles up as our coroutine handle |
| 4865 | auto coroData = coro::createCoroutineData(); |
| 4866 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4867 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4868 | if(coroData->useInternalScheduler) |
| 4869 | { |
| 4870 | ::getOrCreateScheduler().bind(); |
| 4871 | } |
| 4872 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4873 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4874 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4875 | // any fiber switch occurs. |
| 4876 | coro::setHandleParam(coroData); |
| 4877 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4878 | ASSERT(!coroData->routineFiber); |
| 4879 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4880 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4881 | beginFunc(); |
| 4882 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4883 | ASSERT(coroData->inRoutine); |
| 4884 | coroData->done = true; // coroutine is done. |
| 4885 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4886 | coroData->inRoutine = false; |
| 4887 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4888 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4889 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4890 | ASSERT(!coroData->mainFiber); |
| 4891 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4892 | |
| 4893 | // block until the first yield or coroutine end |
| 4894 | ASSERT(!coroData->inRoutine); |
| 4895 | coroData->inRoutine = true; |
| 4896 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4897 | while(coroData->inRoutine) |
| 4898 | { |
| 4899 | coroData->mainFiber->wait(); |
| 4900 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4901 | |
| 4902 | return coroData; |
| 4903 | } |
| 4904 | |
| 4905 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4906 | { |
| 4907 | // Start by creating a regular function |
| 4908 | createFunction(yieldType, params); |
| 4909 | |
| 4910 | // Save in case yield() is called |
| 4911 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4912 | ::coroYieldType = yieldType; |
| 4913 | } |
| 4914 | |
| 4915 | void Nucleus::yield(Value *val) |
| 4916 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4917 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4918 | Variable::materializeAll(); |
| 4919 | |
| 4920 | // On first yield, we start generating coroutine functions |
| 4921 | if(!::coroGen) |
| 4922 | { |
| 4923 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4924 | ::coroGen->generateCoroutineBegin(); |
| 4925 | } |
| 4926 | |
| 4927 | ASSERT(::coroGen); |
| 4928 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4929 | } |
| 4930 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4931 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4932 | { |
| 4933 | return false; |
| 4934 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4935 | |
| 4936 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4937 | { |
| 4938 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4939 | |
| 4940 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 4941 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4942 | if(::coroGen) |
| 4943 | { |
| 4944 | // Finish generating coroutine functions |
| 4945 | { |
| 4946 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4947 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4948 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4949 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4950 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4951 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4952 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4953 | // At this point, we no longer need the CoroutineGenerator. |
| 4954 | ::coroGen.reset(); |
| 4955 | ::coroYieldType = nullptr; |
| 4956 | |
| 4957 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4958 | { name, "await", "destroy" }, |
| 4959 | cfgEdit); |
| 4960 | |
| 4961 | return routine; |
| 4962 | } |
| 4963 | else |
| 4964 | { |
| 4965 | { |
| 4966 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4967 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | ::coroYieldType = nullptr; |
| 4971 | |
| 4972 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 4973 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 4974 | |
| 4975 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 4976 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 4977 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 4978 | return routine; |
| 4979 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4980 | } |
| 4981 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4982 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4983 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4984 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 4985 | |
| 4986 | if(isCoroutine) |
| 4987 | { |
| 4988 | return rr::invokeCoroutineBegin(func); |
| 4989 | } |
| 4990 | else |
| 4991 | { |
| 4992 | // For regular routines, just invoke the begin func directly |
| 4993 | return func(); |
| 4994 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4995 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4996 | |
| 4997 | } // namespace rr |