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" |
| 35 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 36 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 37 | #include "marl/event.h" |
| 38 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 39 | #if __has_feature(memory_sanitizer) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 40 | # include <sanitizer/msan_interface.h> |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 41 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 42 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 43 | #if defined(_WIN32) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 44 | # ifndef WIN32_LEAN_AND_MEAN |
| 45 | # define WIN32_LEAN_AND_MEAN |
| 46 | # endif // !WIN32_LEAN_AND_MEAN |
| 47 | # ifndef NOMINMAX |
| 48 | # define NOMINMAX |
| 49 | # endif // !NOMINMAX |
| 50 | # include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 51 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 52 | |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 53 | #include <array> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 54 | #include <iostream> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 55 | #include <limits> |
| 56 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 57 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 58 | // Subzero utility functions |
| 59 | // These functions only accept and return Subzero (Ice) types, and do not access any globals. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 60 | namespace { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 61 | namespace sz { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 62 | |
| 63 | Ice::Cfg *createFunction(Ice::GlobalContext *context, Ice::Type returnType, const std::vector<Ice::Type> ¶mTypes) |
| 64 | { |
| 65 | uint32_t sequenceNumber = 0; |
| 66 | auto function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 67 | |
| 68 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 69 | |
| 70 | for(auto type : paramTypes) |
| 71 | { |
| 72 | Ice::Variable *arg = function->makeVariable(type); |
| 73 | function->addArg(arg); |
| 74 | } |
| 75 | |
| 76 | Ice::CfgNode *node = function->makeNode(); |
| 77 | function->setEntryNode(node); |
| 78 | |
| 79 | return function; |
| 80 | } |
| 81 | |
| 82 | Ice::Type getPointerType(Ice::Type elementType) |
| 83 | { |
| 84 | if(sizeof(void *) == 8) |
| 85 | { |
| 86 | return Ice::IceType_i64; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | return Ice::IceType_i32; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 95 | { |
| 96 | int typeSize = Ice::typeWidthInBytes(type); |
| 97 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 98 | |
| 99 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 100 | auto address = function->makeVariable(getPointerType(type)); |
| 101 | auto alloca = Ice::InstAlloca::create(function, address, bytes, typeSize); |
| 102 | function->getEntryNode()->getInsts().push_front(alloca); |
| 103 | |
| 104 | return address; |
| 105 | } |
| 106 | |
| 107 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 108 | { |
| 109 | if(sizeof(void *) == 8) |
| 110 | { |
| 111 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 116 | } |
| 117 | } |
| 118 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 119 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 120 | 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] | 121 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 122 | // Wrapper for calls on C functions with Ice types |
| 123 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 124 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 125 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 126 | |
| 127 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 128 | // then truncate result to bool. |
| 129 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 130 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 131 | if(returningBool) |
| 132 | { |
| 133 | ret = function->makeVariable(Ice::IceType_i32); |
| 134 | } |
| 135 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 136 | { |
| 137 | ret = function->makeVariable(retTy); |
| 138 | } |
| 139 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 140 | 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] | 141 | for(auto arg : iceArgs) |
| 142 | { |
| 143 | call->addArg(arg); |
| 144 | } |
| 145 | |
| 146 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 147 | |
| 148 | if(returningBool) |
| 149 | { |
| 150 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 151 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 152 | } |
| 153 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 157 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 158 | { |
| 159 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 160 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 161 | } |
| 162 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 163 | // Wrapper for calls on C functions with Ice types |
| 164 | template<typename Return, typename... CArgs, typename... RArgs> |
| 165 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Return(fptr)(CArgs...), RArgs &&... args) |
| 166 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 167 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 168 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 169 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 170 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 171 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 174 | // Returns a non-const variable copy of const v |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 175 | Ice::Variable *createUnconstCast(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Constant *v) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 176 | { |
| 177 | Ice::Variable *result = function->makeVariable(v->getType()); |
| 178 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Bitcast, result, v); |
| 179 | basicBlock->appendInst(cast); |
| 180 | return result; |
| 181 | } |
| 182 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 183 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 184 | { |
| 185 | Ice::Variable *to = function->makeVariable(toType); |
| 186 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 187 | basicBlock->appendInst(cast); |
| 188 | return to; |
| 189 | } |
| 190 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 191 | 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] | 192 | { |
| 193 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 194 | // absolute address. We circumvent this by casting to a non-const variable, and loading |
| 195 | // from that. |
| 196 | if(auto *cptr = llvm::dyn_cast<Ice::Constant>(ptr)) |
| 197 | { |
| 198 | ptr = sz::createUnconstCast(function, basicBlock, cptr); |
| 199 | } |
| 200 | |
| 201 | Ice::Variable *result = function->makeVariable(type); |
| 202 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 203 | basicBlock->appendInst(load); |
| 204 | |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 209 | } // namespace |
| 210 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 211 | namespace rr { |
| 212 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 213 | class CoroutineGenerator; |
| 214 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 215 | |
| 216 | namespace { |
| 217 | |
| 218 | // Default configuration settings. Must be accessed under mutex lock. |
| 219 | std::mutex defaultConfigLock; |
| 220 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 221 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 222 | // This uses a static in a function to avoid the cost of a global static |
| 223 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 224 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 225 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 226 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 229 | Ice::GlobalContext *context = nullptr; |
| 230 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 231 | Ice::CfgNode *entryBlock = nullptr; |
| 232 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 233 | Ice::CfgNode *basicBlock = nullptr; |
| 234 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 235 | rr::ELFMemoryStreamer *routine = nullptr; |
| 236 | |
| 237 | std::mutex codegenMutex; |
| 238 | |
| 239 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 240 | Ice::Fdstream *out = nullptr; |
| 241 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 242 | // Coroutine globals |
| 243 | rr::Type *coroYieldType = nullptr; |
| 244 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 245 | marl::Scheduler &getOrCreateScheduler() |
| 246 | { |
| 247 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 248 | marl::Scheduler::Config cfg; |
| 249 | cfg.setWorkerThreadCount(8); |
| 250 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 251 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 252 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 253 | return *scheduler; |
| 254 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 255 | } // Anonymous namespace |
| 256 | |
| 257 | namespace { |
| 258 | |
| 259 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 260 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 261 | #endif |
| 262 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 263 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 264 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 265 | #endif |
| 266 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 267 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 268 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 269 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 270 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 271 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 272 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 273 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 274 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 275 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 276 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 277 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 278 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 281 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 282 | { |
| 283 | switch(memoryOrder) |
| 284 | { |
| 285 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 286 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 287 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 288 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 289 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 290 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
| 291 | } |
| 292 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 293 | } |
| 294 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 295 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 296 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 297 | public: |
| 298 | const static bool ARM; |
| 299 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 300 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 301 | private: |
| 302 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 303 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 304 | #if defined(__i386__) || defined(__x86_64__) |
| 305 | # if defined(_WIN32) |
| 306 | __cpuid(registers, info); |
| 307 | # else |
| 308 | __asm volatile("cpuid" |
| 309 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 310 | : "a"(info)); |
| 311 | # endif |
| 312 | #else |
| 313 | registers[0] = 0; |
| 314 | registers[1] = 0; |
| 315 | registers[2] = 0; |
| 316 | registers[3] = 0; |
| 317 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 320 | static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 321 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 322 | #if defined(__arm__) || defined(__aarch64__) |
| 323 | return true; |
| 324 | #elif defined(__i386__) || defined(__x86_64__) |
| 325 | return false; |
| 326 | #elif defined(__mips__) |
| 327 | return false; |
| 328 | #else |
| 329 | # error "Unknown architecture" |
| 330 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 331 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 332 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 333 | static bool detectSSE4_1() |
| 334 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 335 | #if defined(__i386__) || defined(__x86_64__) |
| 336 | int registers[4]; |
| 337 | cpuid(registers, 1); |
| 338 | return (registers[2] & 0x00080000) != 0; |
| 339 | #else |
| 340 | return false; |
| 341 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 342 | } |
| 343 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 344 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 345 | const bool CPUID::ARM = CPUID::detectARM(); |
| 346 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
| 347 | const bool emulateIntrinsics = false; |
| 348 | const bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 349 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 350 | constexpr bool subzeroDumpEnabled = false; |
| 351 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 352 | |
| 353 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 354 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 355 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 356 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 357 | |
| 358 | } // anonymous namespace |
| 359 | |
| 360 | namespace rr { |
| 361 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 362 | std::string BackendName() |
| 363 | { |
| 364 | return "Subzero"; |
| 365 | } |
| 366 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 367 | const Capabilities Caps = { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 368 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | enum EmulatedType |
| 372 | { |
| 373 | EmulatedShift = 16, |
| 374 | EmulatedV2 = 2 << EmulatedShift, |
| 375 | EmulatedV4 = 4 << EmulatedShift, |
| 376 | EmulatedV8 = 8 << EmulatedShift, |
| 377 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 378 | |
| 379 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 380 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 381 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 382 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 383 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 384 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 385 | }; |
| 386 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 387 | class Value : public Ice::Operand |
| 388 | {}; |
| 389 | class SwitchCases : public Ice::InstSwitch |
| 390 | {}; |
| 391 | class BasicBlock : public Ice::CfgNode |
| 392 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 393 | |
| 394 | Ice::Type T(Type *t) |
| 395 | { |
| 396 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 397 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 400 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 401 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 402 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | Type *T(EmulatedType t) |
| 406 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 407 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 410 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 411 | { |
| 412 | std::vector<Ice::Type> result; |
| 413 | result.reserve(types.size()); |
| 414 | for(auto &t : types) |
| 415 | { |
| 416 | result.push_back(T(t)); |
| 417 | } |
| 418 | return result; |
| 419 | } |
| 420 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 421 | Value *V(Ice::Operand *v) |
| 422 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 423 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 426 | Ice::Operand *V(Value *v) |
| 427 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 428 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 431 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 432 | { |
| 433 | std::vector<Ice::Operand *> result; |
| 434 | result.reserve(values.size()); |
| 435 | for(auto &v : values) |
| 436 | { |
| 437 | result.push_back(V(v)); |
| 438 | } |
| 439 | return result; |
| 440 | } |
| 441 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 442 | BasicBlock *B(Ice::CfgNode *b) |
| 443 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 444 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static size_t typeSize(Type *type) |
| 448 | { |
| 449 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 450 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 451 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 452 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 453 | case Type_v2i32: return 8; |
| 454 | case Type_v4i16: return 8; |
| 455 | case Type_v2i16: return 4; |
| 456 | case Type_v8i8: return 8; |
| 457 | case Type_v4i8: return 4; |
| 458 | case Type_v2f32: return 8; |
| 459 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | return Ice::typeWidthInBytes(T(type)); |
| 464 | } |
| 465 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 466 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 467 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 468 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 469 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 470 | { |
| 471 | Nucleus::createRetVoid(); |
| 472 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 473 | |
| 474 | // Connect the entry block to the top of the initial basic block |
| 475 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 476 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 477 | } |
| 478 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 479 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 480 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 481 | |
| 482 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 483 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 484 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 488 | { |
| 489 | return §ionHeader(elfHeader)[index]; |
| 490 | } |
| 491 | |
| 492 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 493 | { |
| 494 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 495 | |
| 496 | uint32_t index = relocation.getSymbol(); |
| 497 | int table = relocationTable.sh_link; |
| 498 | void *symbolValue = nullptr; |
| 499 | |
| 500 | if(index != SHN_UNDEF) |
| 501 | { |
| 502 | if(table == SHN_UNDEF) return nullptr; |
| 503 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 504 | |
| 505 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 506 | if(index >= symtab_entries) |
| 507 | { |
| 508 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 509 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 512 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 513 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 514 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 515 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 516 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 517 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 518 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 519 | 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] | 520 | } |
| 521 | else |
| 522 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 523 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 524 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 528 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 529 | |
| 530 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 531 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 532 | switch(relocation.getType()) |
| 533 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 534 | case R_ARM_NONE: |
| 535 | // No relocation |
| 536 | break; |
| 537 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 538 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 539 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 540 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 541 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 542 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 543 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 544 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 545 | { |
| 546 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 547 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 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 | default: |
| 551 | ASSERT(false && "Unsupported relocation type"); |
| 552 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 553 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 554 | } |
| 555 | else |
| 556 | { |
| 557 | switch(relocation.getType()) |
| 558 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 559 | case R_386_NONE: |
| 560 | // No relocation |
| 561 | break; |
| 562 | case R_386_32: |
| 563 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 564 | break; |
| 565 | case R_386_PC32: |
| 566 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 567 | break; |
| 568 | default: |
| 569 | ASSERT(false && "Unsupported relocation type"); |
| 570 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 571 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 572 | } |
| 573 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 574 | return symbolValue; |
| 575 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 576 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 577 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 578 | { |
| 579 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 580 | |
| 581 | uint32_t index = relocation.getSymbol(); |
| 582 | int table = relocationTable.sh_link; |
| 583 | void *symbolValue = nullptr; |
| 584 | |
| 585 | if(index != SHN_UNDEF) |
| 586 | { |
| 587 | if(table == SHN_UNDEF) return nullptr; |
| 588 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 589 | |
| 590 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 591 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 592 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 593 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 594 | return nullptr; |
| 595 | } |
| 596 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 597 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 598 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 599 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 600 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 601 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 602 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 603 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 604 | 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] | 605 | } |
| 606 | else |
| 607 | { |
| 608 | return nullptr; |
| 609 | } |
| 610 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 611 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 612 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 613 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 614 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 615 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 616 | switch(relocation.getType()) |
| 617 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 618 | case R_X86_64_NONE: |
| 619 | // No relocation |
| 620 | break; |
| 621 | case R_X86_64_64: |
| 622 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 623 | break; |
| 624 | case R_X86_64_PC32: |
| 625 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 626 | break; |
| 627 | case R_X86_64_32S: |
| 628 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 629 | break; |
| 630 | default: |
| 631 | ASSERT(false && "Unsupported relocation type"); |
| 632 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | return symbolValue; |
| 636 | } |
| 637 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 638 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 639 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 640 | const void *entry; |
| 641 | size_t codeSize = 0; |
| 642 | }; |
| 643 | |
| 644 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 645 | { |
| 646 | ASSERT(functionNames.size() > 0); |
| 647 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 648 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 649 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 650 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 651 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 652 | if(!elfHeader->checkMagic()) |
| 653 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 654 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 658 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 659 | #if defined(__i386__) |
| 660 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 661 | #elif defined(__x86_64__) |
| 662 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 663 | #elif defined(__arm__) |
| 664 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 665 | #elif defined(__aarch64__) |
| 666 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 667 | #elif defined(__mips__) |
| 668 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 669 | #else |
| 670 | # error "Unsupported platform" |
| 671 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 672 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 673 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 674 | |
| 675 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 676 | { |
| 677 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 678 | { |
| 679 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 680 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 681 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 682 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 683 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 684 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 685 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 686 | { |
| 687 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 688 | { |
| 689 | return j; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 694 | return static_cast<size_t>(-1); |
| 695 | }; |
| 696 | |
| 697 | size_t index = findSectionNameEntryIndex(); |
| 698 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 699 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 700 | } |
| 701 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 702 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 703 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 704 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 705 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 706 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 707 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 708 | 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] | 709 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 710 | } |
| 711 | } |
| 712 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 713 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 714 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 715 | |
| 716 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 717 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 718 | 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] | 719 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 724 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | template<typename T> |
| 728 | struct ExecutableAllocator |
| 729 | { |
| 730 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 731 | template<class U> |
| 732 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 733 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 734 | |
| 735 | using value_type = T; |
| 736 | using size_type = std::size_t; |
| 737 | |
| 738 | T *allocate(size_type n) |
| 739 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 740 | return (T *)allocateMemoryPages( |
| 741 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void deallocate(T *p, size_type n) |
| 745 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 746 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 747 | } |
| 748 | }; |
| 749 | |
| 750 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 751 | { |
| 752 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 753 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 754 | |
| 755 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 756 | ELFMemoryStreamer() |
| 757 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 758 | { |
| 759 | position = 0; |
| 760 | buffer.reserve(0x1000); |
| 761 | } |
| 762 | |
| 763 | ~ELFMemoryStreamer() override |
| 764 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void write8(uint8_t Value) override |
| 768 | { |
| 769 | if(position == (uint64_t)buffer.size()) |
| 770 | { |
| 771 | buffer.push_back(Value); |
| 772 | position++; |
| 773 | } |
| 774 | else if(position < (uint64_t)buffer.size()) |
| 775 | { |
| 776 | buffer[position] = Value; |
| 777 | position++; |
| 778 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 779 | else |
| 780 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void writeBytes(llvm::StringRef Bytes) override |
| 784 | { |
| 785 | std::size_t oldSize = buffer.size(); |
| 786 | buffer.resize(oldSize + Bytes.size()); |
| 787 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 788 | position += Bytes.size(); |
| 789 | } |
| 790 | |
| 791 | uint64_t tell() const override { return position; } |
| 792 | |
| 793 | void seek(uint64_t Off) override { position = Off; } |
| 794 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 795 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 796 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 797 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 798 | |
| 799 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 800 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 801 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 802 | for(auto &entryPoint : entryPoints) |
| 803 | { |
| 804 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 805 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 806 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 807 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 808 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 811 | void finalize() |
| 812 | { |
| 813 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 814 | |
| 815 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 816 | } |
| 817 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 818 | void setEntry(int index, const void *func) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 819 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 820 | ASSERT(func); |
| 821 | funcs[index] = func; |
| 822 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 823 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 824 | const void *getEntry(int index) const override |
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(funcs[index]); |
| 827 | return funcs[index]; |
| 828 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 829 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 830 | 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] | 831 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 832 | // TODO(b/148086935): Replace with a buffer allocator. |
| 833 | size_t space = size + alignment; |
| 834 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 835 | void *ptr = buf.get(); |
| 836 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 837 | ASSERT(alignedPtr); |
| 838 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 839 | constantData.emplace_back(std::move(buf)); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 840 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 841 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 842 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 843 | private: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 844 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 845 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 846 | std::size_t position; |
| 847 | std::vector<std::unique_ptr<uint8_t[]>> constantData; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 848 | }; |
| 849 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 850 | #ifdef ENABLE_RR_PRINT |
| 851 | void VPrintf(const std::vector<Value *> &vals) |
| 852 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 853 | 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] | 854 | } |
| 855 | #endif // ENABLE_RR_PRINT |
| 856 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 857 | Nucleus::Nucleus() |
| 858 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 859 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 860 | |
| 861 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 862 | Ice::ClFlags::getParsedClFlags(Flags); |
| 863 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 864 | #if defined(__arm__) |
| 865 | Flags.setTargetArch(Ice::Target_ARM32); |
| 866 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 867 | #elif defined(__mips__) |
| 868 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 869 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 870 | #else // x86 |
| 871 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 872 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 873 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 874 | Flags.setOutFileType(Ice::FT_Elf); |
| 875 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
| 876 | Flags.setApplicationBinaryInterface(Ice::ABI_Platform); |
| 877 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 878 | Flags.setDisableHybridAssembly(true); |
| 879 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 880 | // Emit functions into separate sections in the ELF so we can find them by name |
| 881 | Flags.setFunctionSections(true); |
| 882 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 883 | static llvm::raw_os_ostream cout(std::cout); |
| 884 | static llvm::raw_os_ostream cerr(std::cerr); |
| 885 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 886 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 887 | { |
| 888 | // Decorate text asm with liveness info |
| 889 | Flags.setDecorateAsm(true); |
| 890 | } |
| 891 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 892 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 893 | { |
| 894 | std::error_code errorCode; |
| 895 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 896 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 897 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 898 | } |
| 899 | else |
| 900 | { |
| 901 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 902 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 903 | ::routine = elfMemory; |
| 904 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 905 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 906 | #if !__has_feature(memory_sanitizer) |
| 907 | // thread_local variables in shared libraries are initialized at load-time, |
| 908 | // but this is not observed by MemorySanitizer if the loader itself was not |
| 909 | // instrumented, leading to false-positive unitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 910 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 911 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 912 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | Nucleus::~Nucleus() |
| 916 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 917 | delete Variable::unmaterializedVariables; |
| 918 | Variable::unmaterializedVariables = nullptr; |
| 919 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 920 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 921 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 922 | |
| 923 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 924 | ::allocator = nullptr; |
| 925 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 926 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 927 | ::function = nullptr; |
| 928 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 929 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 930 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 931 | |
| 932 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 933 | ::elfFile = nullptr; |
| 934 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 935 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 936 | ::out = nullptr; |
| 937 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 938 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 939 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 940 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 941 | |
| 942 | ::codegenMutex.unlock(); |
| 943 | } |
| 944 | |
| 945 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 946 | { |
| 947 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 948 | ::defaultConfig() = cfg; |
| 949 | } |
| 950 | |
| 951 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 952 | { |
| 953 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 954 | auto &config = ::defaultConfig(); |
| 955 | config = cfgEdit.apply(config); |
| 956 | } |
| 957 | |
| 958 | Config Nucleus::getDefaultConfig() |
| 959 | { |
| 960 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 961 | return ::defaultConfig(); |
| 962 | } |
| 963 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 964 | // This function lowers and produces executable binary code in memory for the input functions, |
| 965 | // and returns a Routine with the entry points to these functions. |
| 966 | template<size_t Count> |
| 967 | 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] | 968 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 969 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 970 | // and GlobalContext::emitItems. |
| 971 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 972 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 973 | { |
| 974 | // 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] | 975 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | ::context->emitFileHeader(); |
| 979 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 980 | // Translate |
| 981 | |
| 982 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 983 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 984 | Ice::Cfg *currFunc = functions[i]; |
| 985 | |
| 986 | // Install function allocator in TLS for Cfg-specific container allocators |
| 987 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 988 | |
| 989 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 990 | |
| 991 | rr::optimize(currFunc); |
| 992 | |
| 993 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 994 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 995 | |
| 996 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 997 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 998 | |
| 999 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1000 | |
| 1001 | if(subzeroEmitTextAsm) |
| 1002 | { |
| 1003 | currFunc->emit(); |
| 1004 | } |
| 1005 | |
| 1006 | currFunc->emitIAS(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1007 | } |
| 1008 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1009 | // Emit items |
| 1010 | |
| 1011 | ::context->lowerGlobals(""); |
| 1012 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1013 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1014 | |
| 1015 | for(size_t i = 0; i < Count; ++i) |
| 1016 | { |
| 1017 | Ice::Cfg *currFunc = functions[i]; |
| 1018 | |
| 1019 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1020 | auto globals = currFunc->getGlobalInits(); |
| 1021 | if(globals && !globals->empty()) |
| 1022 | { |
| 1023 | ::context->getGlobals()->merge(globals.get()); |
| 1024 | } |
| 1025 | |
| 1026 | auto assembler = currFunc->releaseAssembler(); |
| 1027 | assembler->alignFunction(); |
| 1028 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1029 | } |
| 1030 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1031 | ::context->lowerGlobals("last"); |
| 1032 | ::context->lowerConstants(); |
| 1033 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1034 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1035 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1036 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1037 | objectWriter->writeNonUserSections(); |
| 1038 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1039 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1040 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1041 | ASSERT(entryPoints.size() == Count); |
| 1042 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1043 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1044 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1048 | |
| 1049 | Routine *handoffRoutine = ::routine; |
| 1050 | ::routine = nullptr; |
| 1051 | |
| 1052 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1053 | } |
| 1054 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1055 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 1056 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1057 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1058 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1059 | } |
| 1060 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1061 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1062 | { |
| 1063 | Ice::Type type = T(t); |
| 1064 | int typeSize = Ice::typeWidthInBytes(type); |
| 1065 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1066 | |
| 1067 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1068 | auto address = ::function->makeVariable(T(getPointerType(t))); |
| 1069 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); |
| 1070 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1071 | |
| 1072 | return V(address); |
| 1073 | } |
| 1074 | |
| 1075 | BasicBlock *Nucleus::createBasicBlock() |
| 1076 | { |
| 1077 | return B(::function->makeNode()); |
| 1078 | } |
| 1079 | |
| 1080 | BasicBlock *Nucleus::getInsertBlock() |
| 1081 | { |
| 1082 | return B(::basicBlock); |
| 1083 | } |
| 1084 | |
| 1085 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1086 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1087 | // 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] | 1088 | |
| 1089 | Variable::materializeAll(); |
| 1090 | |
| 1091 | ::basicBlock = basicBlock; |
| 1092 | } |
| 1093 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1094 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1095 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1096 | ASSERT(::function == nullptr); |
| 1097 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1098 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1099 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1100 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1101 | |
| 1102 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1103 | |
| 1104 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1105 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1106 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1107 | // 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] | 1108 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1109 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1110 | ::entryBlock = ::function->getEntryNode(); |
| 1111 | ::basicBlock = ::function->makeNode(); |
| 1112 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | Value *Nucleus::getArgument(unsigned int index) |
| 1116 | { |
| 1117 | return V(::function->getArgs()[index]); |
| 1118 | } |
| 1119 | |
| 1120 | void Nucleus::createRetVoid() |
| 1121 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1122 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1123 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1124 | // Code generated after this point is unreachable, so any variables |
| 1125 | // being read can safely return an undefined value. We have to avoid |
| 1126 | // materializing variables after the terminator ret instruction. |
| 1127 | Variable::killUnmaterialized(); |
| 1128 | |
| 1129 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1130 | ::basicBlock->appendInst(ret); |
| 1131 | } |
| 1132 | |
| 1133 | void Nucleus::createRet(Value *v) |
| 1134 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1135 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1136 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1137 | // Code generated after this point is unreachable, so any variables |
| 1138 | // being read can safely return an undefined value. We have to avoid |
| 1139 | // materializing variables after the terminator ret instruction. |
| 1140 | Variable::killUnmaterialized(); |
| 1141 | |
| 1142 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1143 | ::basicBlock->appendInst(ret); |
| 1144 | } |
| 1145 | |
| 1146 | void Nucleus::createBr(BasicBlock *dest) |
| 1147 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1148 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1149 | Variable::materializeAll(); |
| 1150 | |
| 1151 | auto br = Ice::InstBr::create(::function, dest); |
| 1152 | ::basicBlock->appendInst(br); |
| 1153 | } |
| 1154 | |
| 1155 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1156 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1157 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1158 | Variable::materializeAll(); |
| 1159 | |
| 1160 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1161 | ::basicBlock->appendInst(br); |
| 1162 | } |
| 1163 | |
| 1164 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1165 | { |
| 1166 | switch(op) |
| 1167 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1168 | case Ice::InstArithmetic::Add: |
| 1169 | case Ice::InstArithmetic::Fadd: |
| 1170 | case Ice::InstArithmetic::Mul: |
| 1171 | case Ice::InstArithmetic::Fmul: |
| 1172 | case Ice::InstArithmetic::And: |
| 1173 | case Ice::InstArithmetic::Or: |
| 1174 | case Ice::InstArithmetic::Xor: |
| 1175 | return true; |
| 1176 | default: |
| 1177 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1182 | { |
| 1183 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1184 | |
| 1185 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1186 | |
| 1187 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1188 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1189 | ::basicBlock->appendInst(arithmetic); |
| 1190 | |
| 1191 | return V(result); |
| 1192 | } |
| 1193 | |
| 1194 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1195 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1196 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1197 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
| 1198 | } |
| 1199 | |
| 1200 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1201 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1202 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1203 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
| 1204 | } |
| 1205 | |
| 1206 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1207 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1208 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1209 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
| 1210 | } |
| 1211 | |
| 1212 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1213 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1214 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1215 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
| 1216 | } |
| 1217 | |
| 1218 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1219 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1220 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1221 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
| 1222 | } |
| 1223 | |
| 1224 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1225 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1226 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1227 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
| 1228 | } |
| 1229 | |
| 1230 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1231 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1232 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1233 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
| 1234 | } |
| 1235 | |
| 1236 | Value *Nucleus::createFMul(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::Fmul, lhs, rhs); |
| 1240 | } |
| 1241 | |
| 1242 | Value *Nucleus::createFDiv(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::Fdiv, lhs, rhs); |
| 1246 | } |
| 1247 | |
| 1248 | Value *Nucleus::createURem(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::Urem, lhs, rhs); |
| 1252 | } |
| 1253 | |
| 1254 | Value *Nucleus::createSRem(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::Srem, lhs, rhs); |
| 1258 | } |
| 1259 | |
| 1260 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1261 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1263 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1264 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1265 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1266 | return nullptr; |
| 1267 | } |
| 1268 | |
| 1269 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1270 | { |
| 1271 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1275 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1276 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1277 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
| 1278 | } |
| 1279 | |
| 1280 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1281 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1282 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1283 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
| 1284 | } |
| 1285 | |
| 1286 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1287 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1288 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1289 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
| 1290 | } |
| 1291 | |
| 1292 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1293 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1294 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1295 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
| 1296 | } |
| 1297 | |
| 1298 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1299 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1300 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1301 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
| 1302 | } |
| 1303 | |
| 1304 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1305 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1306 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1307 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1308 | } |
| 1309 | |
| 1310 | Value *Nucleus::createNeg(Value *v) |
| 1311 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1313 | return createSub(createNullValue(T(v->getType())), v); |
| 1314 | } |
| 1315 | |
| 1316 | Value *Nucleus::createFNeg(Value *v) |
| 1317 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1318 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1319 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1320 | 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] | 1321 | |
| 1322 | return createFSub(negativeZero, v); |
| 1323 | } |
| 1324 | |
| 1325 | Value *Nucleus::createNot(Value *v) |
| 1326 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1327 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1328 | if(Ice::isScalarIntegerType(v->getType())) |
| 1329 | { |
| 1330 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1331 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1332 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1333 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1334 | 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] | 1335 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1336 | } |
| 1337 | } |
| 1338 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1339 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1340 | { |
| 1341 | #if defined(__i386__) || defined(__x86_64__) |
| 1342 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1343 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1344 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1345 | #else |
| 1346 | if(atomic) |
| 1347 | { |
| 1348 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1349 | } |
| 1350 | if(memoryOrder != std::memory_order_relaxed) |
| 1351 | { |
| 1352 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1353 | } |
| 1354 | #endif |
| 1355 | |
| 1356 | // Vulkan doesn't allow sequential memory order |
| 1357 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1358 | } |
| 1359 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1360 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1361 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1362 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1363 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1364 | |
| 1365 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1366 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1367 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1368 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1369 | { |
| 1370 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1371 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1372 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1373 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1374 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1375 | Int x = *Pointer<Int>(pointer); |
| 1376 | |
| 1377 | Int4 vector; |
| 1378 | vector = Insert(vector, x, 0); |
| 1379 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1380 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1381 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1382 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1383 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1384 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1385 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1386 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1387 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1388 | Int x = *Pointer<Int>(pointer); |
| 1389 | Int y = *Pointer<Int>(pointer + 4); |
| 1390 | |
| 1391 | Int4 vector; |
| 1392 | vector = Insert(vector, x, 0); |
| 1393 | vector = Insert(vector, y, 1); |
| 1394 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1395 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1396 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1397 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1398 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1399 | else |
| 1400 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1401 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1402 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1403 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1404 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1405 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1406 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1407 | auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1408 | load->addArg(ptr); |
| 1409 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1410 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1411 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1412 | } |
| 1413 | else |
| 1414 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1415 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1417 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1418 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1419 | return V(result); |
| 1420 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1421 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1422 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1423 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1424 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1425 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1426 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1427 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1428 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1429 | if(align != 0) |
| 1430 | { |
| 1431 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1432 | call->addArg(ptr); |
| 1433 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1434 | ::basicBlock->appendInst(call); |
| 1435 | } |
| 1436 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1437 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1438 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1439 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1440 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1441 | { |
| 1442 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1443 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1444 | if(typeSize(type) == 4) |
| 1445 | { |
| 1446 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1447 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1448 | ::basicBlock->appendInst(bitcast); |
| 1449 | |
| 1450 | RValue<Int4> v(V(vector)); |
| 1451 | |
| 1452 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1453 | Int x = Extract(v, 0); |
| 1454 | *Pointer<Int>(pointer) = x; |
| 1455 | } |
| 1456 | else if(typeSize(type) == 8) |
| 1457 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1458 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1459 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1460 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1461 | ::basicBlock->appendInst(bitcast); |
| 1462 | |
| 1463 | RValue<Int4> v(V(vector)); |
| 1464 | |
| 1465 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1466 | Int x = Extract(v, 0); |
| 1467 | *Pointer<Int>(pointer) = x; |
| 1468 | Int y = Extract(v, 1); |
| 1469 | *Pointer<Int>(pointer + 4) = y; |
| 1470 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1471 | else |
| 1472 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1473 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1474 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1475 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1476 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1477 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1478 | auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic); |
| 1479 | store->addArg(value); |
| 1480 | store->addArg(ptr); |
| 1481 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1482 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1483 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1484 | } |
| 1485 | else |
| 1486 | { |
| 1487 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1488 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1489 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1490 | ::basicBlock->appendInst(store); |
| 1491 | } |
| 1492 | |
| 1493 | return value; |
| 1494 | } |
| 1495 | |
| 1496 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1497 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1498 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1499 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1500 | |
| 1501 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1502 | { |
| 1503 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1504 | |
| 1505 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1506 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1507 | return ptr; |
| 1508 | } |
| 1509 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1510 | return createAdd(ptr, createConstantInt(offset)); |
| 1511 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1512 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1513 | if(!Ice::isByteSizedType(T(type))) |
| 1514 | { |
| 1515 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1516 | } |
| 1517 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1518 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1519 | { |
| 1520 | if(unsignedIndex) |
| 1521 | { |
| 1522 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1523 | } |
| 1524 | else |
| 1525 | { |
| 1526 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | return createAdd(ptr, index); |
| 1531 | } |
| 1532 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1533 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1534 | { |
| 1535 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1536 | |
| 1537 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicRMW, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1538 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1539 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1540 | auto op = ::context->getConstantInt32(rmwOp); |
| 1541 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1542 | inst->addArg(op); |
| 1543 | inst->addArg(ptr); |
| 1544 | inst->addArg(value); |
| 1545 | inst->addArg(order); |
| 1546 | ::basicBlock->appendInst(inst); |
| 1547 | |
| 1548 | return V(result); |
| 1549 | } |
| 1550 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1551 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1552 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1553 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1554 | return createAtomicRMW(Ice::Intrinsics::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1558 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1559 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1560 | return createAtomicRMW(Ice::Intrinsics::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1564 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1565 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1566 | return createAtomicRMW(Ice::Intrinsics::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1570 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1571 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1572 | return createAtomicRMW(Ice::Intrinsics::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1576 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1577 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1578 | return createAtomicRMW(Ice::Intrinsics::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1582 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1583 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1584 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1588 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1589 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1590 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1591 | |
| 1592 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicCmpxchg, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1593 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1594 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1595 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1596 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1597 | inst->addArg(ptr); |
| 1598 | inst->addArg(compare); |
| 1599 | inst->addArg(value); |
| 1600 | inst->addArg(orderEq); |
| 1601 | inst->addArg(orderNeq); |
| 1602 | ::basicBlock->appendInst(inst); |
| 1603 | |
| 1604 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1608 | { |
| 1609 | if(v->getType() == T(destType)) |
| 1610 | { |
| 1611 | return v; |
| 1612 | } |
| 1613 | |
| 1614 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1615 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1616 | ::basicBlock->appendInst(cast); |
| 1617 | |
| 1618 | return V(result); |
| 1619 | } |
| 1620 | |
| 1621 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1622 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1623 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1624 | return createCast(Ice::InstCast::Trunc, v, destType); |
| 1625 | } |
| 1626 | |
| 1627 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1628 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1629 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1630 | return createCast(Ice::InstCast::Zext, v, destType); |
| 1631 | } |
| 1632 | |
| 1633 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1634 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1635 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1636 | return createCast(Ice::InstCast::Sext, v, destType); |
| 1637 | } |
| 1638 | |
| 1639 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1640 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1641 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1642 | return createCast(Ice::InstCast::Fptoui, v, destType); |
| 1643 | } |
| 1644 | |
| 1645 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1646 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1647 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1648 | return createCast(Ice::InstCast::Fptosi, v, destType); |
| 1649 | } |
| 1650 | |
| 1651 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1652 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1653 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1654 | return createCast(Ice::InstCast::Sitofp, v, destType); |
| 1655 | } |
| 1656 | |
| 1657 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1658 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1659 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1660 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
| 1661 | } |
| 1662 | |
| 1663 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1664 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1665 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1666 | return createCast(Ice::InstCast::Fpext, v, destType); |
| 1667 | } |
| 1668 | |
| 1669 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1670 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1671 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1672 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1673 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1674 | // emulate them by writing to the stack and reading back as the destination type. |
| 1675 | if(emulateMismatchedBitCast) |
| 1676 | { |
| 1677 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1678 | { |
| 1679 | Value *address = allocateStackVariable(destType); |
| 1680 | createStore(v, address, T(v->getType())); |
| 1681 | return createLoad(address, destType); |
| 1682 | } |
| 1683 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1684 | { |
| 1685 | Value *address = allocateStackVariable(T(v->getType())); |
| 1686 | createStore(v, address, T(v->getType())); |
| 1687 | return createLoad(address, destType); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1692 | } |
| 1693 | |
| 1694 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1695 | { |
| 1696 | ASSERT(lhs->getType() == rhs->getType()); |
| 1697 | |
| 1698 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1699 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1700 | ::basicBlock->appendInst(cmp); |
| 1701 | |
| 1702 | return V(result); |
| 1703 | } |
| 1704 | |
| 1705 | Value *Nucleus::createPtrEQ(Value *lhs, Value *rhs) |
| 1706 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1707 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1708 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1709 | } |
| 1710 | |
| 1711 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1712 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1713 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1714 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1715 | } |
| 1716 | |
| 1717 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1718 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1719 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1720 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1721 | } |
| 1722 | |
| 1723 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1724 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1725 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1726 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1727 | } |
| 1728 | |
| 1729 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1730 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1731 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1732 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1733 | } |
| 1734 | |
| 1735 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1736 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1737 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1738 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1739 | } |
| 1740 | |
| 1741 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1742 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1743 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1744 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1745 | } |
| 1746 | |
| 1747 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1748 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1749 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1750 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1751 | } |
| 1752 | |
| 1753 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1754 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1755 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1756 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1757 | } |
| 1758 | |
| 1759 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1760 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1761 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1762 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1763 | } |
| 1764 | |
| 1765 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1766 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1767 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1768 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1769 | } |
| 1770 | |
| 1771 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1772 | { |
| 1773 | ASSERT(lhs->getType() == rhs->getType()); |
| 1774 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1775 | |
| 1776 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1777 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1778 | ::basicBlock->appendInst(cmp); |
| 1779 | |
| 1780 | return V(result); |
| 1781 | } |
| 1782 | |
| 1783 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1784 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1785 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1786 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
| 1787 | } |
| 1788 | |
| 1789 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1790 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1791 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1792 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
| 1793 | } |
| 1794 | |
| 1795 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1796 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1797 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1798 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
| 1799 | } |
| 1800 | |
| 1801 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1802 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1803 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1804 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
| 1805 | } |
| 1806 | |
| 1807 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1808 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1809 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1810 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
| 1811 | } |
| 1812 | |
| 1813 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1814 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1815 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1816 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
| 1817 | } |
| 1818 | |
| 1819 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1820 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1821 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1822 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
| 1823 | } |
| 1824 | |
| 1825 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1826 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1827 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1828 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
| 1829 | } |
| 1830 | |
| 1831 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1832 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1833 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1834 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
| 1835 | } |
| 1836 | |
| 1837 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1838 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1839 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1840 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
| 1841 | } |
| 1842 | |
| 1843 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1844 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1845 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1846 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
| 1847 | } |
| 1848 | |
| 1849 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1850 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1851 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1852 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
| 1853 | } |
| 1854 | |
| 1855 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1856 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1857 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1858 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
| 1859 | } |
| 1860 | |
| 1861 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1862 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1863 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1864 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1865 | } |
| 1866 | |
| 1867 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1868 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1869 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1870 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1871 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1872 | ::basicBlock->appendInst(extract); |
| 1873 | |
| 1874 | return V(result); |
| 1875 | } |
| 1876 | |
| 1877 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1878 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1879 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1880 | auto result = ::function->makeVariable(vector->getType()); |
| 1881 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1882 | ::basicBlock->appendInst(insert); |
| 1883 | |
| 1884 | return V(result); |
| 1885 | } |
| 1886 | |
| 1887 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 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 | ASSERT(V1->getType() == V2->getType()); |
| 1891 | |
| 1892 | int size = Ice::typeNumElements(V1->getType()); |
| 1893 | auto result = ::function->makeVariable(V1->getType()); |
| 1894 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1895 | |
| 1896 | for(int i = 0; i < size; i++) |
| 1897 | { |
| 1898 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1899 | } |
| 1900 | |
| 1901 | ::basicBlock->appendInst(shuffle); |
| 1902 | |
| 1903 | return V(result); |
| 1904 | } |
| 1905 | |
| 1906 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1907 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1908 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1909 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1910 | |
| 1911 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1912 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1913 | ::basicBlock->appendInst(select); |
| 1914 | |
| 1915 | return V(result); |
| 1916 | } |
| 1917 | |
| 1918 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1919 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1920 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1921 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1922 | ::basicBlock->appendInst(switchInst); |
| 1923 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1924 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1928 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1929 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1930 | switchCases->addBranch(label, label, branch); |
| 1931 | } |
| 1932 | |
| 1933 | void Nucleus::createUnreachable() |
| 1934 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1935 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1936 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1937 | ::basicBlock->appendInst(unreachable); |
| 1938 | } |
| 1939 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1940 | Type *Nucleus::getType(Value *value) |
| 1941 | { |
| 1942 | return T(V(value)->getType()); |
| 1943 | } |
| 1944 | |
| 1945 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1946 | { |
| 1947 | Ice::Type vecTy = T(vectorType); |
| 1948 | switch(vecTy) |
| 1949 | { |
| 1950 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1951 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1952 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1953 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1954 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1955 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1956 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1957 | default: |
| 1958 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 1959 | return {}; |
| 1960 | } |
| 1961 | } |
| 1962 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1963 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1964 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1965 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1966 | } |
| 1967 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1968 | static constexpr Ice::Type getNaturalIntType() |
| 1969 | { |
| 1970 | constexpr size_t intSize = sizeof(int); |
| 1971 | static_assert(intSize == 4 || intSize == 8, ""); |
| 1972 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 1973 | } |
| 1974 | |
| 1975 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 1976 | { |
| 1977 | Ice::Type valueTy = T(valueType); |
| 1978 | switch(valueTy) |
| 1979 | { |
| 1980 | case Ice::IceType_i32: |
| 1981 | return T(getNaturalIntType()); |
| 1982 | |
| 1983 | case Ice::IceType_f32: |
| 1984 | return T(Ice::IceType_f64); |
| 1985 | |
| 1986 | default: |
| 1987 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 1988 | return {}; |
| 1989 | } |
| 1990 | } |
| 1991 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1992 | Value *Nucleus::createNullValue(Type *Ty) |
| 1993 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1994 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1995 | if(Ice::isVectorType(T(Ty))) |
| 1996 | { |
| 1997 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1998 | 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] | 1999 | return createConstantVector(c, Ty); |
| 2000 | } |
| 2001 | else |
| 2002 | { |
| 2003 | return V(::context->getConstantZero(T(Ty))); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | Value *Nucleus::createConstantLong(int64_t i) |
| 2008 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2009 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2010 | return V(::context->getConstantInt64(i)); |
| 2011 | } |
| 2012 | |
| 2013 | Value *Nucleus::createConstantInt(int i) |
| 2014 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2015 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2016 | return V(::context->getConstantInt32(i)); |
| 2017 | } |
| 2018 | |
| 2019 | Value *Nucleus::createConstantInt(unsigned int i) |
| 2020 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2021 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2022 | return V(::context->getConstantInt32(i)); |
| 2023 | } |
| 2024 | |
| 2025 | Value *Nucleus::createConstantBool(bool b) |
| 2026 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2027 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2028 | return V(::context->getConstantInt1(b)); |
| 2029 | } |
| 2030 | |
| 2031 | Value *Nucleus::createConstantByte(signed char i) |
| 2032 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2033 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2034 | return V(::context->getConstantInt8(i)); |
| 2035 | } |
| 2036 | |
| 2037 | Value *Nucleus::createConstantByte(unsigned char i) |
| 2038 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2039 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2040 | return V(::context->getConstantInt8(i)); |
| 2041 | } |
| 2042 | |
| 2043 | Value *Nucleus::createConstantShort(short i) |
| 2044 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2045 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2046 | return V(::context->getConstantInt16(i)); |
| 2047 | } |
| 2048 | |
| 2049 | Value *Nucleus::createConstantShort(unsigned short i) |
| 2050 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2051 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2052 | return V(::context->getConstantInt16(i)); |
| 2053 | } |
| 2054 | |
| 2055 | Value *Nucleus::createConstantFloat(float x) |
| 2056 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2057 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2058 | return V(::context->getConstantFloat(x)); |
| 2059 | } |
| 2060 | |
| 2061 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2062 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2063 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2064 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2065 | } |
| 2066 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2067 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2068 | { |
| 2069 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2070 | } |
| 2071 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2072 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2073 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2074 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2075 | const int vectorSize = 16; |
| 2076 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2077 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2078 | |
| 2079 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2080 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2081 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2082 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2083 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2084 | |
| 2085 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2086 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2087 | case Ice::IceType_v4i32: |
| 2088 | case Ice::IceType_v4i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2089 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2090 | 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] | 2091 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2092 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2093 | } |
| 2094 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2095 | case Ice::IceType_v4f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2096 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2097 | 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] | 2098 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2099 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2100 | } |
| 2101 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2102 | case Ice::IceType_v8i16: |
| 2103 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2104 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2105 | 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] | 2106 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2107 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2108 | } |
| 2109 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2110 | case Ice::IceType_v16i8: |
| 2111 | case Ice::IceType_v16i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2112 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2113 | 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] | 2114 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2115 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2116 | } |
| 2117 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2118 | case Type_v2i32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2119 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2120 | 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] | 2121 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2122 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2123 | } |
| 2124 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2125 | case Type_v2f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2126 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2127 | 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] | 2128 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2129 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2130 | } |
| 2131 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2132 | case Type_v4i16: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2133 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2134 | 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] | 2135 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2136 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2137 | } |
| 2138 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2139 | case Type_v8i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2140 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2141 | 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] | 2142 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2143 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2144 | } |
| 2145 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2146 | case Type_v4i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2147 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2148 | 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] | 2149 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2150 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2151 | } |
| 2152 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2153 | default: |
| 2154 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2155 | } |
| 2156 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2157 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2158 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2159 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2160 | return V(result); |
| 2161 | } |
| 2162 | |
| 2163 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2164 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2165 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2166 | } |
| 2167 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2168 | Value *Nucleus::createConstantString(const char *v) |
| 2169 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2170 | // 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] | 2171 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2172 | } |
| 2173 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2174 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2175 | { |
| 2176 | return T(Ice::IceType_void); |
| 2177 | } |
| 2178 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2179 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2180 | { |
| 2181 | return T(Ice::IceType_i1); |
| 2182 | } |
| 2183 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2184 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2185 | { |
| 2186 | return T(Ice::IceType_i8); |
| 2187 | } |
| 2188 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2189 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2190 | { |
| 2191 | return T(Ice::IceType_i8); |
| 2192 | } |
| 2193 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2194 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2195 | { |
| 2196 | return T(Ice::IceType_i16); |
| 2197 | } |
| 2198 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2199 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2200 | { |
| 2201 | return T(Ice::IceType_i16); |
| 2202 | } |
| 2203 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2204 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2205 | { |
| 2206 | return T(Type_v4i8); |
| 2207 | } |
| 2208 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2209 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2210 | { |
| 2211 | return T(Type_v4i8); |
| 2212 | } |
| 2213 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2214 | namespace { |
| 2215 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2216 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2217 | 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] | 2218 | } |
| 2219 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2220 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2221 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2222 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2226 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2227 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } // namespace |
| 2230 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2231 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2232 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2233 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2234 | if(emulateIntrinsics) |
| 2235 | { |
| 2236 | Byte8 result; |
| 2237 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2238 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2239 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2240 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2241 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2242 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2243 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2244 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2245 | |
| 2246 | return result; |
| 2247 | } |
| 2248 | else |
| 2249 | { |
| 2250 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2251 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2252 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2253 | auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2254 | paddusb->addArg(x.value()); |
| 2255 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2256 | ::basicBlock->appendInst(paddusb); |
| 2257 | |
| 2258 | return RValue<Byte8>(V(result)); |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2263 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2264 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2265 | if(emulateIntrinsics) |
| 2266 | { |
| 2267 | Byte8 result; |
| 2268 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2269 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2270 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2271 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2272 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2273 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2274 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2275 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2276 | |
| 2277 | return result; |
| 2278 | } |
| 2279 | else |
| 2280 | { |
| 2281 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2282 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2283 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2284 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2285 | psubusw->addArg(x.value()); |
| 2286 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2287 | ::basicBlock->appendInst(psubusw); |
| 2288 | |
| 2289 | return RValue<Byte8>(V(result)); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 2294 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2295 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2296 | return RValue<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2300 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2301 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2302 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2306 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2307 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2308 | if(emulateIntrinsics) |
| 2309 | { |
| 2310 | SByte8 result; |
| 2311 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2312 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2313 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2314 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2315 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2316 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2317 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2318 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2319 | |
| 2320 | return result; |
| 2321 | } |
| 2322 | else |
| 2323 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2324 | #if defined(__i386__) || defined(__x86_64__) |
| 2325 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2326 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2327 | 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] | 2328 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2329 | return As<SByte8>(hi | lo); |
| 2330 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2331 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2332 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2333 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2334 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2335 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2336 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2337 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2338 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2339 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2340 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2341 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2342 | 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] | 2343 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2344 | else |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2345 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2346 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2347 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2348 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2349 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2350 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2351 | ::basicBlock->appendInst(movmsk); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2352 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2353 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2354 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2355 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2356 | |
| 2357 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2358 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2359 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2360 | // } |
| 2361 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2362 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2363 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2364 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2365 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2366 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2367 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2368 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2369 | { |
| 2370 | return T(Type_v8i8); |
| 2371 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2372 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2373 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2374 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2375 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2376 | // } |
| 2377 | |
| 2378 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2379 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2380 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2381 | // } |
| 2382 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2383 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 2384 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2385 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2386 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2387 | } |
| 2388 | |
| 2389 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2390 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2391 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2392 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2393 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2394 | SByte8 result; |
| 2395 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2396 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2397 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2398 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2399 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2400 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2401 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2402 | 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] | 2403 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2404 | return result; |
| 2405 | } |
| 2406 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2407 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2408 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2409 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2410 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2411 | auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2412 | paddsb->addArg(x.value()); |
| 2413 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2414 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2415 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2416 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2417 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2418 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2419 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2420 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2421 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2422 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2423 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2424 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2425 | SByte8 result; |
| 2426 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2427 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2428 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2429 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2430 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2431 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2432 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2433 | 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] | 2434 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2435 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2436 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2437 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2438 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2439 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2440 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2441 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2442 | auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2443 | psubsb->addArg(x.value()); |
| 2444 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2445 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2446 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2447 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2448 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2449 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2450 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2451 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2452 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2453 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2454 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2455 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2456 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2457 | 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] | 2458 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2459 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2460 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2461 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2462 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2463 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2464 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2465 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2466 | ::basicBlock->appendInst(movmsk); |
| 2467 | |
| 2468 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2469 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2470 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2471 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2472 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 2473 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2474 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2475 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2476 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2477 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2478 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2479 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2480 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2481 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
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 | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2484 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2485 | { |
| 2486 | return T(Type_v8i8); |
| 2487 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2488 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2489 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2490 | { |
| 2491 | return T(Ice::IceType_v16i8); |
| 2492 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2493 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2494 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2495 | { |
| 2496 | return T(Ice::IceType_v16i8); |
| 2497 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2498 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2499 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2500 | { |
| 2501 | return T(Type_v2i16); |
| 2502 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2503 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2504 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2505 | { |
| 2506 | return T(Type_v2i16); |
| 2507 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2508 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2509 | Short4::Short4(RValue<Int4> cast) |
| 2510 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2511 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2512 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2513 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2514 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2515 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2516 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2517 | |
| 2518 | storeValue(short4); |
| 2519 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2520 | |
| 2521 | // Short4::Short4(RValue<Float> cast) |
| 2522 | // { |
| 2523 | // } |
| 2524 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2525 | Short4::Short4(RValue<Float4> cast) |
| 2526 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2527 | // TODO(b/150791192): Generalize and optimize |
| 2528 | auto smin = std::numeric_limits<short>::min(); |
| 2529 | auto smax = std::numeric_limits<short>::max(); |
| 2530 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2534 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2535 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2536 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2537 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2538 | Short4 result; |
| 2539 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2540 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2541 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2542 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2543 | |
| 2544 | return result; |
| 2545 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2546 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2547 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2548 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2549 | } |
| 2550 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2551 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2552 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2553 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2554 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2555 | if(emulateIntrinsics) |
| 2556 | { |
| 2557 | Short4 result; |
| 2558 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2559 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2560 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2561 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2562 | |
| 2563 | return result; |
| 2564 | } |
| 2565 | else |
| 2566 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2567 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2572 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2573 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2574 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2575 | 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] | 2576 | ::basicBlock->appendInst(cmp); |
| 2577 | |
| 2578 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2579 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2580 | ::basicBlock->appendInst(select); |
| 2581 | |
| 2582 | return RValue<Short4>(V(result)); |
| 2583 | } |
| 2584 | |
| 2585 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2586 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2587 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2588 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2589 | 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] | 2590 | ::basicBlock->appendInst(cmp); |
| 2591 | |
| 2592 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2593 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2594 | ::basicBlock->appendInst(select); |
| 2595 | |
| 2596 | return RValue<Short4>(V(result)); |
| 2597 | } |
| 2598 | |
| 2599 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 2600 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2601 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2602 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2603 | } |
| 2604 | |
| 2605 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2606 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2607 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2608 | if(emulateIntrinsics) |
| 2609 | { |
| 2610 | Short4 result; |
| 2611 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2612 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2613 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2614 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2615 | |
| 2616 | return result; |
| 2617 | } |
| 2618 | else |
| 2619 | { |
| 2620 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2621 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2622 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2623 | auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2624 | paddsw->addArg(x.value()); |
| 2625 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2626 | ::basicBlock->appendInst(paddsw); |
| 2627 | |
| 2628 | return RValue<Short4>(V(result)); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2633 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2634 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2635 | if(emulateIntrinsics) |
| 2636 | { |
| 2637 | Short4 result; |
| 2638 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2639 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2640 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2641 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2642 | |
| 2643 | return result; |
| 2644 | } |
| 2645 | else |
| 2646 | { |
| 2647 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2648 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2649 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2650 | auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2651 | psubsw->addArg(x.value()); |
| 2652 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2653 | ::basicBlock->appendInst(psubsw); |
| 2654 | |
| 2655 | return RValue<Short4>(V(result)); |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2660 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2661 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2662 | if(emulateIntrinsics) |
| 2663 | { |
| 2664 | Short4 result; |
| 2665 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2666 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2667 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2668 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2669 | |
| 2670 | return result; |
| 2671 | } |
| 2672 | else |
| 2673 | { |
| 2674 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2675 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2676 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2677 | auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2678 | pmulhw->addArg(x.value()); |
| 2679 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2680 | ::basicBlock->appendInst(pmulhw); |
| 2681 | |
| 2682 | return RValue<Short4>(V(result)); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2687 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2688 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2689 | if(emulateIntrinsics) |
| 2690 | { |
| 2691 | Int2 result; |
| 2692 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2693 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2694 | |
| 2695 | return result; |
| 2696 | } |
| 2697 | else |
| 2698 | { |
| 2699 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2700 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2701 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2702 | auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2703 | pmaddwd->addArg(x.value()); |
| 2704 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2705 | ::basicBlock->appendInst(pmaddwd); |
| 2706 | |
| 2707 | return As<Int2>(V(result)); |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2712 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2713 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2714 | if(emulateIntrinsics) |
| 2715 | { |
| 2716 | SByte8 result; |
| 2717 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2718 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2719 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2720 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2721 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2722 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2723 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2724 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2725 | |
| 2726 | return result; |
| 2727 | } |
| 2728 | else |
| 2729 | { |
| 2730 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2731 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2732 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2733 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2734 | pack->addArg(x.value()); |
| 2735 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2736 | ::basicBlock->appendInst(pack); |
| 2737 | |
| 2738 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2743 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2744 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2745 | if(emulateIntrinsics) |
| 2746 | { |
| 2747 | Byte8 result; |
| 2748 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2749 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2750 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2751 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2752 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2753 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2754 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2755 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2756 | |
| 2757 | return result; |
| 2758 | } |
| 2759 | else |
| 2760 | { |
| 2761 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2762 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2763 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2764 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2765 | pack->addArg(x.value()); |
| 2766 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2767 | ::basicBlock->appendInst(pack); |
| 2768 | |
| 2769 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2774 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2775 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2776 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2780 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2781 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2782 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2783 | } |
| 2784 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2785 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2786 | { |
| 2787 | return T(Type_v4i16); |
| 2788 | } |
| 2789 | |
| 2790 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2791 | { |
| 2792 | if(saturate) |
| 2793 | { |
| 2794 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2795 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2796 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2797 | // PackUnsigned takes care of 0x0000 saturation. |
| 2798 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2799 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2800 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2801 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2802 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2803 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2804 | Int4 int4(cast); |
| 2805 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2806 | } |
| 2807 | else |
| 2808 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2809 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2810 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2811 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2812 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2813 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2814 | *this = Short4(Int4(cast)); |
| 2815 | } |
| 2816 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2817 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2818 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2819 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2820 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2824 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2825 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2829 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2830 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2831 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2832 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2833 | { |
| 2834 | UShort4 result; |
| 2835 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2836 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2837 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2838 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2839 | |
| 2840 | return result; |
| 2841 | } |
| 2842 | else |
| 2843 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2844 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2849 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2850 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2851 | if(emulateIntrinsics) |
| 2852 | { |
| 2853 | UShort4 result; |
| 2854 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2855 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2856 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2857 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2858 | |
| 2859 | return result; |
| 2860 | } |
| 2861 | else |
| 2862 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2863 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2868 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2869 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2870 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2871 | 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] | 2872 | ::basicBlock->appendInst(cmp); |
| 2873 | |
| 2874 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2875 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2876 | ::basicBlock->appendInst(select); |
| 2877 | |
| 2878 | return RValue<UShort4>(V(result)); |
| 2879 | } |
| 2880 | |
| 2881 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2882 | { |
| 2883 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2884 | 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] | 2885 | ::basicBlock->appendInst(cmp); |
| 2886 | |
| 2887 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2888 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2889 | ::basicBlock->appendInst(select); |
| 2890 | |
| 2891 | return RValue<UShort4>(V(result)); |
| 2892 | } |
| 2893 | |
| 2894 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 2895 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2896 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2897 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2898 | } |
| 2899 | |
| 2900 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2901 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2902 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2903 | if(emulateIntrinsics) |
| 2904 | { |
| 2905 | UShort4 result; |
| 2906 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2907 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2908 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2909 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2910 | |
| 2911 | return result; |
| 2912 | } |
| 2913 | else |
| 2914 | { |
| 2915 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2916 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2917 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2918 | auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2919 | paddusw->addArg(x.value()); |
| 2920 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2921 | ::basicBlock->appendInst(paddusw); |
| 2922 | |
| 2923 | return RValue<UShort4>(V(result)); |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2928 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2929 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2930 | if(emulateIntrinsics) |
| 2931 | { |
| 2932 | UShort4 result; |
| 2933 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2934 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2935 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2936 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2937 | |
| 2938 | return result; |
| 2939 | } |
| 2940 | else |
| 2941 | { |
| 2942 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2943 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2944 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2945 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2946 | psubusw->addArg(x.value()); |
| 2947 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2948 | ::basicBlock->appendInst(psubusw); |
| 2949 | |
| 2950 | return RValue<UShort4>(V(result)); |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2955 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2956 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2957 | if(emulateIntrinsics) |
| 2958 | { |
| 2959 | UShort4 result; |
| 2960 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2961 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2962 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2963 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 2964 | |
| 2965 | return result; |
| 2966 | } |
| 2967 | else |
| 2968 | { |
| 2969 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2970 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2971 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2972 | auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2973 | pmulhuw->addArg(x.value()); |
| 2974 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2975 | ::basicBlock->appendInst(pmulhuw); |
| 2976 | |
| 2977 | return RValue<UShort4>(V(result)); |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2982 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2983 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2984 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2985 | |
| 2986 | // Scalarized implementation. |
| 2987 | Int4 result; |
| 2988 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 2989 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 2990 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 2991 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 2992 | |
| 2993 | return result; |
| 2994 | } |
| 2995 | |
| 2996 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 2997 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2998 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2999 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3000 | |
| 3001 | if(false) // Partial product based implementation. |
| 3002 | { |
| 3003 | auto xh = x >> 16; |
| 3004 | auto yh = y >> 16; |
| 3005 | auto xl = x & UInt4(0x0000FFFF); |
| 3006 | auto yl = y & UInt4(0x0000FFFF); |
| 3007 | auto xlyh = xl * yh; |
| 3008 | auto xhyl = xh * yl; |
| 3009 | auto xlyhh = xlyh >> 16; |
| 3010 | auto xhylh = xhyl >> 16; |
| 3011 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3012 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3013 | auto xlylh = (xl * yl) >> 16; |
| 3014 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3015 | |
| 3016 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3017 | } |
| 3018 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3019 | // Scalarized implementation. |
| 3020 | Int4 result; |
| 3021 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3022 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3023 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3024 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3025 | |
| 3026 | return As<UInt4>(result); |
| 3027 | } |
| 3028 | |
| 3029 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3030 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3031 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3032 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3033 | return UShort4(0); |
| 3034 | } |
| 3035 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3036 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3037 | { |
| 3038 | return T(Type_v4i16); |
| 3039 | } |
| 3040 | |
| 3041 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 3042 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3043 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3044 | return RValue<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3048 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3049 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3050 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3054 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3055 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3056 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3057 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3058 | Short8 result; |
| 3059 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3060 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3061 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3062 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3063 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3064 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3065 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3066 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3067 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3068 | return result; |
| 3069 | } |
| 3070 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3071 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3072 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3073 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3074 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3075 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 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 | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3094 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3095 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3096 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3097 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3098 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3099 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3100 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3101 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3102 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3103 | return Int4(0); |
| 3104 | } |
| 3105 | |
| 3106 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 3107 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3108 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3109 | UNIMPLEMENTED_NO_BUG("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3110 | return Short8(0); |
| 3111 | } |
| 3112 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3113 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3114 | { |
| 3115 | return T(Ice::IceType_v8i16); |
| 3116 | } |
| 3117 | |
| 3118 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 3119 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3120 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3121 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3125 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3126 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3127 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3131 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3132 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3133 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3134 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3135 | UShort8 result; |
| 3136 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3137 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3138 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3139 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3140 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3141 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3142 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3143 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3144 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3145 | return result; |
| 3146 | } |
| 3147 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3148 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3149 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3150 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3151 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3152 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 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 | 598f8d8 | 2016-09-26 15:09:10 -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; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3169 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 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::createLShr(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> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3177 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3178 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3179 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3180 | return UShort8(0); |
| 3181 | } |
| 3182 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3183 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3184 | { |
| 3185 | return T(Ice::IceType_v8i16); |
| 3186 | } |
| 3187 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3188 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3189 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3190 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3191 | RValue<Int> res = val; |
| 3192 | val += 1; |
| 3193 | return res; |
| 3194 | } |
| 3195 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3196 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3197 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3198 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3199 | val += 1; |
| 3200 | return val; |
| 3201 | } |
| 3202 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3203 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3204 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3205 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3206 | RValue<Int> res = val; |
| 3207 | val -= 1; |
| 3208 | return res; |
| 3209 | } |
| 3210 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3211 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3212 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3213 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3214 | val -= 1; |
| 3215 | return val; |
| 3216 | } |
| 3217 | |
| 3218 | RValue<Int> RoundInt(RValue<Float> cast) |
| 3219 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3220 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3221 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3222 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3223 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3224 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3225 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3226 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3227 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3228 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3229 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3230 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3231 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3232 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3233 | ::basicBlock->appendInst(nearbyint); |
| 3234 | |
| 3235 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3236 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3237 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3238 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3239 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3240 | { |
| 3241 | return T(Ice::IceType_i32); |
| 3242 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3243 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3244 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3245 | { |
| 3246 | return T(Ice::IceType_i64); |
| 3247 | } |
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 | UInt::UInt(RValue<Float> cast) |
| 3250 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3251 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3252 | // Smallest positive value representable in UInt, but not in Int |
| 3253 | const unsigned int ustart = 0x80000000u; |
| 3254 | const float ustartf = float(ustart); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3255 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3256 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3257 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3258 | // Check if the value can be represented as an Int |
| 3259 | IfThenElse(cast >= ustartf, |
| 3260 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3261 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3262 | // Otherwise, just convert normally |
| 3263 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3264 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3265 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3266 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3267 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3268 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3269 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3270 | RValue<UInt> res = val; |
| 3271 | val += 1; |
| 3272 | return res; |
| 3273 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3274 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3275 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3276 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3277 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3278 | val += 1; |
| 3279 | return val; |
| 3280 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3281 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3282 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3283 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3284 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3285 | RValue<UInt> res = val; |
| 3286 | val -= 1; |
| 3287 | return res; |
| 3288 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3289 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3290 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3291 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3292 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3293 | val -= 1; |
| 3294 | return val; |
| 3295 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3296 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3297 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3298 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3299 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3300 | // } |
| 3301 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3302 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3303 | { |
| 3304 | return T(Ice::IceType_i32); |
| 3305 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3306 | |
| 3307 | // Int2::Int2(RValue<Int> cast) |
| 3308 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3309 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3310 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3311 | // |
| 3312 | // Constant *shuffle[2]; |
| 3313 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3314 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3315 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3316 | // 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] | 3317 | // |
| 3318 | // storeValue(replicate); |
| 3319 | // } |
| 3320 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3321 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3322 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3323 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3324 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3325 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3326 | Int2 result; |
| 3327 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3328 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3329 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3330 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3331 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3332 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3333 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3334 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3335 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3336 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3337 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3338 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3339 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3340 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3341 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3342 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3343 | Int2 result; |
| 3344 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3345 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3346 | |
| 3347 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3348 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3349 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3350 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3351 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3352 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3353 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3354 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3355 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3356 | { |
| 3357 | return T(Type_v2i32); |
| 3358 | } |
| 3359 | |
| 3360 | RValue<UInt2> operator<<(RValue<UInt2> 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 | UInt2 result; |
| 3366 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3367 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3368 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 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<UInt2>(Nucleus::createShl(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 | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3377 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3378 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3379 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3380 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3381 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3382 | UInt2 result; |
| 3383 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3384 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3385 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3386 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3387 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3388 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3389 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3390 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3391 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3392 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3393 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3394 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3395 | { |
| 3396 | return T(Type_v2i32); |
| 3397 | } |
| 3398 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3399 | Int4::Int4(RValue<Byte4> cast) |
| 3400 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3401 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3402 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3403 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3404 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3405 | |
| 3406 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3407 | 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] | 3408 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3409 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3410 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3411 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3412 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3413 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3414 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3415 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3416 | storeValue(f); |
| 3417 | } |
| 3418 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3419 | Int4::Int4(RValue<SByte4> cast) |
| 3420 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3421 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3422 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3423 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3424 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3425 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3426 | 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] | 3427 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3428 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3429 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3430 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3431 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3432 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3433 | |
| 3434 | *this = As<Int4>(e) >> 24; |
| 3435 | } |
| 3436 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3437 | Int4::Int4(RValue<Short4> cast) |
| 3438 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3439 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3440 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3441 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3442 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3443 | |
| 3444 | *this = As<Int4>(c) >> 16; |
| 3445 | } |
| 3446 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3447 | Int4::Int4(RValue<UShort4> cast) |
| 3448 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3449 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3450 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3451 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3452 | 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] | 3453 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3454 | storeValue(d); |
| 3455 | } |
| 3456 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3457 | Int4::Int4(RValue<Int> rhs) |
| 3458 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3459 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3460 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3461 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3462 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3463 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3464 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3465 | |
| 3466 | storeValue(replicate); |
| 3467 | } |
| 3468 | |
| 3469 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3470 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3471 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3472 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3473 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3474 | Int4 result; |
| 3475 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3476 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3477 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3478 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3479 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3480 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3481 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3482 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3483 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3484 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3485 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3486 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3487 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3488 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3489 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3490 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3491 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3492 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3493 | Int4 result; |
| 3494 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3495 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3496 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3497 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3498 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3499 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3500 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3501 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3502 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3503 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3504 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3505 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3506 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3507 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3508 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3509 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3510 | return RValue<Int4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3514 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3515 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3516 | return RValue<Int4>(Nucleus::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3520 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3521 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3522 | return RValue<Int4>(Nucleus::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3526 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3527 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3528 | return RValue<Int4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3532 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3533 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3534 | return RValue<Int4>(Nucleus::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3538 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3539 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3540 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3544 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3545 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3546 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3547 | 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] | 3548 | ::basicBlock->appendInst(cmp); |
| 3549 | |
| 3550 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3551 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3552 | ::basicBlock->appendInst(select); |
| 3553 | |
| 3554 | return RValue<Int4>(V(result)); |
| 3555 | } |
| 3556 | |
| 3557 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3558 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3559 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3560 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3561 | 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] | 3562 | ::basicBlock->appendInst(cmp); |
| 3563 | |
| 3564 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3565 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3566 | ::basicBlock->appendInst(select); |
| 3567 | |
| 3568 | return RValue<Int4>(V(result)); |
| 3569 | } |
| 3570 | |
| 3571 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3572 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3573 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3574 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3575 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3576 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3577 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3578 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3579 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3580 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3581 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3582 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3583 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3584 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3585 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3586 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3587 | |
| 3588 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3589 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3590 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3591 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3592 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3593 | { |
| 3594 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3595 | |
| 3596 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3597 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3598 | // saturate to 0x80000000. |
| 3599 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3600 | |
| 3601 | if(emulateIntrinsics || CPUID::ARM) |
| 3602 | { |
| 3603 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3604 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3605 | } |
| 3606 | else |
| 3607 | { |
| 3608 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3609 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 3610 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3611 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3612 | nearbyint->addArg(clamped.value()); |
| 3613 | ::basicBlock->appendInst(nearbyint); |
| 3614 | |
| 3615 | return RValue<Int4>(V(result)); |
| 3616 | } |
| 3617 | } |
| 3618 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3619 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3620 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3621 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3622 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3623 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3624 | Short8 result; |
| 3625 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3626 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3627 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3628 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3629 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3630 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3631 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3632 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3633 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3634 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3635 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3636 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3637 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3638 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3639 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3640 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3641 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3642 | pack->addArg(x.value()); |
| 3643 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3644 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3645 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3646 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3647 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3648 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3649 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3650 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3651 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3652 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3653 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3654 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3655 | RValue<Int4> sx = As<Int4>(x); |
| 3656 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3657 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3658 | RValue<Int4> sy = As<Int4>(y); |
| 3659 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3660 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3661 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3662 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3663 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3664 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3665 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3666 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3667 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3668 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3669 | pack->addArg(x.value()); |
| 3670 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3671 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3672 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3673 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3674 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3675 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3676 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3677 | RValue<Int> SignMask(RValue<Int4> x) |
| 3678 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3679 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3680 | if(emulateIntrinsics || CPUID::ARM) |
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 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3683 | 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] | 3684 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3685 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3686 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3687 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3688 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3689 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3690 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3691 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3692 | ::basicBlock->appendInst(movmsk); |
| 3693 | |
| 3694 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3695 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3696 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3697 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3698 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3699 | { |
| 3700 | return T(Ice::IceType_v4i32); |
| 3701 | } |
| 3702 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3703 | UInt4::UInt4(RValue<Float4> cast) |
| 3704 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3705 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3706 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3707 | // Smallest positive value representable in UInt, but not in Int |
| 3708 | const unsigned int ustart = 0x80000000u; |
| 3709 | const float ustartf = float(ustart); |
| 3710 | |
| 3711 | // Check if the value can be represented as an Int |
| 3712 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3713 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3714 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3715 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3716 | (~uiValue & Int4(cast)); |
| 3717 | // 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] | 3718 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3719 | } |
| 3720 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3721 | UInt4::UInt4(RValue<UInt> rhs) |
| 3722 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3723 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3724 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3725 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3726 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3727 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3728 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3729 | |
| 3730 | storeValue(replicate); |
| 3731 | } |
| 3732 | |
| 3733 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3734 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3735 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3736 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3737 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3738 | UInt4 result; |
| 3739 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3740 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3741 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3742 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3743 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3744 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3745 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3746 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3747 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3748 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3749 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3750 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3751 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3752 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3753 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3754 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3755 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3756 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3757 | UInt4 result; |
| 3758 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3759 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3760 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3761 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3762 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3763 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3764 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3765 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3766 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3767 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3768 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3769 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3770 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3771 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3772 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3773 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3774 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3778 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3779 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3780 | return RValue<UInt4>(Nucleus::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3784 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3785 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3786 | return RValue<UInt4>(Nucleus::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3790 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3791 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3792 | return RValue<UInt4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3796 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3797 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3798 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3802 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3803 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3804 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3808 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3809 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3810 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3811 | 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] | 3812 | ::basicBlock->appendInst(cmp); |
| 3813 | |
| 3814 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3815 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3816 | ::basicBlock->appendInst(select); |
| 3817 | |
| 3818 | return RValue<UInt4>(V(result)); |
| 3819 | } |
| 3820 | |
| 3821 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3822 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3823 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3824 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3825 | 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] | 3826 | ::basicBlock->appendInst(cmp); |
| 3827 | |
| 3828 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3829 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3830 | ::basicBlock->appendInst(select); |
| 3831 | |
| 3832 | return RValue<UInt4>(V(result)); |
| 3833 | } |
| 3834 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3835 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3836 | { |
| 3837 | return T(Ice::IceType_v4i32); |
| 3838 | } |
| 3839 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3840 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3841 | { |
| 3842 | return T(Ice::IceType_i16); |
| 3843 | } |
| 3844 | |
| 3845 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3846 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3847 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3848 | return 1.0f / x; |
| 3849 | } |
| 3850 | |
| 3851 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3852 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3853 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3854 | return Rcp_pp(Sqrt(x)); |
| 3855 | } |
| 3856 | |
| 3857 | RValue<Float> Sqrt(RValue<Float> x) |
| 3858 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3859 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3860 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3861 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3862 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3863 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3864 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3865 | ::basicBlock->appendInst(sqrt); |
| 3866 | |
| 3867 | return RValue<Float>(V(result)); |
| 3868 | } |
| 3869 | |
| 3870 | RValue<Float> Round(RValue<Float> x) |
| 3871 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3872 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3873 | return Float4(Round(Float4(x))).x; |
| 3874 | } |
| 3875 | |
| 3876 | RValue<Float> Trunc(RValue<Float> x) |
| 3877 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3878 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3879 | return Float4(Trunc(Float4(x))).x; |
| 3880 | } |
| 3881 | |
| 3882 | RValue<Float> Frac(RValue<Float> x) |
| 3883 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3884 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3885 | return Float4(Frac(Float4(x))).x; |
| 3886 | } |
| 3887 | |
| 3888 | RValue<Float> Floor(RValue<Float> x) |
| 3889 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3890 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3891 | return Float4(Floor(Float4(x))).x; |
| 3892 | } |
| 3893 | |
| 3894 | RValue<Float> Ceil(RValue<Float> x) |
| 3895 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3896 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3897 | return Float4(Ceil(Float4(x))).x; |
| 3898 | } |
| 3899 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3900 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3901 | { |
| 3902 | return T(Ice::IceType_f32); |
| 3903 | } |
| 3904 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3905 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3906 | { |
| 3907 | return T(Type_v2f32); |
| 3908 | } |
| 3909 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3910 | Float4::Float4(RValue<Float> rhs) |
| 3911 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3912 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3913 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3914 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3915 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3916 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3917 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3918 | |
| 3919 | storeValue(replicate); |
| 3920 | } |
| 3921 | |
| 3922 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3923 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3924 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3925 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3926 | 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] | 3927 | ::basicBlock->appendInst(cmp); |
| 3928 | |
| 3929 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3930 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3931 | ::basicBlock->appendInst(select); |
| 3932 | |
| 3933 | return RValue<Float4>(V(result)); |
| 3934 | } |
| 3935 | |
| 3936 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3937 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3938 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3939 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3940 | 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] | 3941 | ::basicBlock->appendInst(cmp); |
| 3942 | |
| 3943 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3944 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3945 | ::basicBlock->appendInst(select); |
| 3946 | |
| 3947 | return RValue<Float4>(V(result)); |
| 3948 | } |
| 3949 | |
| 3950 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3951 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3952 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3953 | return Float4(1.0f) / x; |
| 3954 | } |
| 3955 | |
| 3956 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3957 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3958 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3959 | return Rcp_pp(Sqrt(x)); |
| 3960 | } |
| 3961 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 3962 | bool HasRcpApprox() |
| 3963 | { |
| 3964 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3965 | return false; |
| 3966 | } |
| 3967 | |
| 3968 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 3969 | { |
| 3970 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3971 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 3972 | return { 0.0f }; |
| 3973 | } |
| 3974 | |
| 3975 | RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2) |
| 3976 | { |
| 3977 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3978 | UNREACHABLE("RValue<Float> RcpApprox()"); |
| 3979 | return { 0.0f }; |
| 3980 | } |
| 3981 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 3982 | bool HasRcpSqrtApprox() |
| 3983 | { |
| 3984 | return false; |
| 3985 | } |
| 3986 | |
| 3987 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 3988 | { |
| 3989 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3990 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 3991 | return { 0.0f }; |
| 3992 | } |
| 3993 | |
| 3994 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 3995 | { |
| 3996 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3997 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 3998 | return { 0.0f }; |
| 3999 | } |
| 4000 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4001 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4002 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4003 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4004 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4005 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4006 | Float4 result; |
| 4007 | result.x = Sqrt(Float(Float4(x).x)); |
| 4008 | result.y = Sqrt(Float(Float4(x).y)); |
| 4009 | result.z = Sqrt(Float(Float4(x).z)); |
| 4010 | result.w = Sqrt(Float(Float4(x).w)); |
| 4011 | |
| 4012 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4013 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4014 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4015 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4016 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4017 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4018 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4019 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4020 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4021 | ::basicBlock->appendInst(sqrt); |
| 4022 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4023 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4024 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4025 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4026 | |
| 4027 | RValue<Int> SignMask(RValue<Float4> x) |
| 4028 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4029 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4030 | if(emulateIntrinsics || CPUID::ARM) |
| 4031 | { |
| 4032 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4033 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4034 | } |
| 4035 | else |
| 4036 | { |
| 4037 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4038 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4039 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4040 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4041 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4042 | ::basicBlock->appendInst(movmsk); |
| 4043 | |
| 4044 | return RValue<Int>(V(result)); |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 4049 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4050 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4051 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 4055 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4056 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4057 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 4061 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4062 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4063 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4067 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4068 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4069 | return RValue<Int4>(Nucleus::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 4073 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4074 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4075 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 4079 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4080 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4081 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 4085 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4086 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4087 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 4091 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4092 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4093 | return RValue<Int4>(Nucleus::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 4097 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4098 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4099 | return RValue<Int4>(Nucleus::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4103 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4104 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4105 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4109 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4110 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4111 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4115 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4116 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4117 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | RValue<Float4> Round(RValue<Float4> x) |
| 4121 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4122 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4123 | if(emulateIntrinsics || CPUID::ARM) |
| 4124 | { |
| 4125 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4126 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4127 | } |
| 4128 | else if(CPUID::SSE4_1) |
| 4129 | { |
| 4130 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4131 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4132 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4133 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4134 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4135 | round->addArg(::context->getConstantInt32(0)); |
| 4136 | ::basicBlock->appendInst(round); |
| 4137 | |
| 4138 | return RValue<Float4>(V(result)); |
| 4139 | } |
| 4140 | else |
| 4141 | { |
| 4142 | return Float4(RoundInt(x)); |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | RValue<Float4> Trunc(RValue<Float4> x) |
| 4147 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4148 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4149 | if(CPUID::SSE4_1) |
| 4150 | { |
| 4151 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4152 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4153 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4154 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4155 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4156 | round->addArg(::context->getConstantInt32(3)); |
| 4157 | ::basicBlock->appendInst(round); |
| 4158 | |
| 4159 | return RValue<Float4>(V(result)); |
| 4160 | } |
| 4161 | else |
| 4162 | { |
| 4163 | return Float4(Int4(x)); |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | RValue<Float4> Frac(RValue<Float4> x) |
| 4168 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4169 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4170 | Float4 frc; |
| 4171 | |
| 4172 | if(CPUID::SSE4_1) |
| 4173 | { |
| 4174 | frc = x - Floor(x); |
| 4175 | } |
| 4176 | else |
| 4177 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4178 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4179 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4180 | 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] | 4181 | } |
| 4182 | |
| 4183 | // x - floor(x) can be 1.0 for very small negative x. |
| 4184 | // Clamp against the value just below 1.0. |
| 4185 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4186 | } |
| 4187 | |
| 4188 | RValue<Float4> Floor(RValue<Float4> x) |
| 4189 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4190 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4191 | if(CPUID::SSE4_1) |
| 4192 | { |
| 4193 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4194 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4195 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4196 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4197 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4198 | round->addArg(::context->getConstantInt32(1)); |
| 4199 | ::basicBlock->appendInst(round); |
| 4200 | |
| 4201 | return RValue<Float4>(V(result)); |
| 4202 | } |
| 4203 | else |
| 4204 | { |
| 4205 | return x - Frac(x); |
| 4206 | } |
| 4207 | } |
| 4208 | |
| 4209 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4210 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4211 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4212 | if(CPUID::SSE4_1) |
| 4213 | { |
| 4214 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4215 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4216 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4217 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4218 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4219 | round->addArg(::context->getConstantInt32(2)); |
| 4220 | ::basicBlock->appendInst(round); |
| 4221 | |
| 4222 | return RValue<Float4>(V(result)); |
| 4223 | } |
| 4224 | else |
| 4225 | { |
| 4226 | return -Floor(-x); |
| 4227 | } |
| 4228 | } |
| 4229 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4230 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4231 | { |
| 4232 | return T(Ice::IceType_v4f32); |
| 4233 | } |
| 4234 | |
| 4235 | RValue<Long> Ticks() |
| 4236 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4237 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4238 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4239 | return Long(Int(0)); |
| 4240 | } |
| 4241 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4242 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4243 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4244 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4245 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4246 | } |
| 4247 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4248 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4249 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4250 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4251 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4252 | } |
| 4253 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4254 | 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] | 4255 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4256 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4257 | 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] | 4258 | } |
| 4259 | |
| 4260 | void Breakpoint() |
| 4261 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4263 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Trap, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4264 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4265 | auto trap = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4266 | ::basicBlock->appendInst(trap); |
| 4267 | } |
| 4268 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4269 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4270 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4271 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4272 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicFence, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4273 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4274 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4275 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4276 | inst->addArg(order); |
| 4277 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4278 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4279 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4280 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4281 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4282 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4283 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4284 | return nullptr; |
| 4285 | } |
| 4286 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4287 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4288 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4289 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4290 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4291 | |
| 4292 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4293 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4294 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4295 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4296 | } |
| 4297 | |
| 4298 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4299 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4300 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4301 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4302 | } |
| 4303 | |
| 4304 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4305 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4306 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4307 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4308 | } |
| 4309 | |
| 4310 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4311 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4313 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4314 | } |
| 4315 | |
| 4316 | RValue<Float> Exp2(RValue<Float> x) |
| 4317 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4318 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4319 | return emulated::Exp2(x); |
| 4320 | } |
| 4321 | |
| 4322 | RValue<Float> Log2(RValue<Float> x) |
| 4323 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4324 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4325 | return emulated::Log2(x); |
| 4326 | } |
| 4327 | |
| 4328 | RValue<Float4> Sin(RValue<Float4> x) |
| 4329 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4330 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4331 | return optimal::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | RValue<Float4> Cos(RValue<Float4> x) |
| 4335 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4337 | return optimal::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | RValue<Float4> Tan(RValue<Float4> x) |
| 4341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4343 | return optimal::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4344 | } |
| 4345 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4346 | RValue<Float4> Asin(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4347 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4348 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4349 | if(p == Precision::Full) |
| 4350 | { |
| 4351 | return emulated::Asin(x); |
| 4352 | } |
| 4353 | return optimal::Asin_8_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4354 | } |
| 4355 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4356 | RValue<Float4> Acos(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4357 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4358 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4359 | // Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial approximation |
| 4360 | // version of acos, unlike for Asin, which requires higher precision algorithms. |
| 4361 | return optimal::Acos_4_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | RValue<Float4> Atan(RValue<Float4> x) |
| 4365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4367 | return optimal::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4371 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4372 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4373 | return optimal::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | RValue<Float4> Cosh(RValue<Float4> x) |
| 4377 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4378 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4379 | return optimal::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | RValue<Float4> Tanh(RValue<Float4> x) |
| 4383 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4384 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4385 | return optimal::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | RValue<Float4> Asinh(RValue<Float4> x) |
| 4389 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4390 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4391 | return optimal::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | RValue<Float4> Acosh(RValue<Float4> x) |
| 4395 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4396 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4397 | return optimal::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4398 | } |
| 4399 | |
| 4400 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4401 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4402 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4403 | return optimal::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 4407 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4408 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4409 | return optimal::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4413 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4415 | return optimal::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | RValue<Float4> Exp(RValue<Float4> x) |
| 4419 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4421 | return optimal::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | RValue<Float4> Log(RValue<Float4> x) |
| 4425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4427 | return optimal::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4431 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4432 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4433 | return optimal::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | RValue<Float4> Log2(RValue<Float4> x) |
| 4437 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4438 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4439 | return optimal::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4445 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4446 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4447 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4448 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4449 | } |
| 4450 | else |
| 4451 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4452 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4453 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Ctlz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4454 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4455 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4456 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4457 | ::basicBlock->appendInst(ctlz); |
| 4458 | |
| 4459 | return RValue<UInt>(V(result)); |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4464 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4465 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4466 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4467 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4468 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4469 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4470 | } |
| 4471 | else |
| 4472 | { |
| 4473 | // TODO: implement vectorized version in Subzero |
| 4474 | UInt4 result; |
| 4475 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4476 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4477 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4478 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4479 | return result; |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4484 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4485 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4486 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4487 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4488 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4489 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4490 | } |
| 4491 | else |
| 4492 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4493 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4494 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Cttz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4495 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4496 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4497 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4498 | ::basicBlock->appendInst(ctlz); |
| 4499 | |
| 4500 | return RValue<UInt>(V(result)); |
| 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4505 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4506 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4507 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4508 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4509 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4510 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4511 | } |
| 4512 | else |
| 4513 | { |
| 4514 | // TODO: implement vectorized version in Subzero |
| 4515 | UInt4 result; |
| 4516 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4517 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4518 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4519 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4520 | return result; |
| 4521 | } |
| 4522 | } |
| 4523 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4524 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4525 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4526 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4527 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4528 | } |
| 4529 | |
| 4530 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> 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<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> 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::MaxAtomic(x, y, memoryOrder); |
| 4540 | } |
| 4541 | |
| 4542 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> 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 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4548 | void EmitDebugLocation() |
| 4549 | { |
| 4550 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4551 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4552 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4553 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4554 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4555 | void FlushDebug() {} |
| 4556 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4557 | namespace { |
| 4558 | namespace coro { |
| 4559 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4560 | // Instance data per generated coroutine |
| 4561 | // This is the "handle" type used for Coroutine functions |
| 4562 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4563 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4564 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4565 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4566 | bool done = false; // the coroutine should stop at the next yield() |
| 4567 | bool terminated = false; // the coroutine has finished. |
| 4568 | bool inRoutine = false; // is the coroutine currently executing? |
| 4569 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4570 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4571 | void *promisePtr = nullptr; |
| 4572 | }; |
| 4573 | |
| 4574 | CoroutineData *createCoroutineData() |
| 4575 | { |
| 4576 | return new CoroutineData{}; |
| 4577 | } |
| 4578 | |
| 4579 | void destroyCoroutineData(CoroutineData *coroData) |
| 4580 | { |
| 4581 | delete coroData; |
| 4582 | } |
| 4583 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4584 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4585 | // caller's call to await(). |
| 4586 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4587 | // is called. |
| 4588 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4589 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4590 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4591 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4592 | ASSERT(coroData->inRoutine); |
| 4593 | coroData->inRoutine = false; |
| 4594 | coroData->mainFiber->notify(); |
| 4595 | while(!coroData->inRoutine) |
| 4596 | { |
| 4597 | coroData->routineFiber->wait(); |
| 4598 | } |
| 4599 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4600 | } |
| 4601 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4602 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4603 | // or the coroutine terminates. |
| 4604 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4605 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4606 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4607 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4608 | ASSERT(!coroData->inRoutine); |
| 4609 | coroData->inRoutine = true; |
| 4610 | coroData->routineFiber->notify(); |
| 4611 | while(coroData->inRoutine) |
| 4612 | { |
| 4613 | coroData->mainFiber->wait(); |
| 4614 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4615 | } |
| 4616 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4617 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4618 | // until the coroutine ends, and deletes the coroutine data. |
| 4619 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4620 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4621 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4622 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4623 | ASSERT(!coroData->inRoutine); |
| 4624 | if(!coroData->terminated) |
| 4625 | { |
| 4626 | coroData->done = true; |
| 4627 | coroData->inRoutine = true; |
| 4628 | coroData->routineFiber->notify(); |
| 4629 | while(!coroData->terminated) |
| 4630 | { |
| 4631 | coroData->mainFiber->wait(); |
| 4632 | } |
| 4633 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4634 | if(coroData->useInternalScheduler) |
| 4635 | { |
| 4636 | ::getOrCreateScheduler().unbind(); |
| 4637 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4638 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | namespace detail { |
| 4642 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4643 | } // namespace detail |
| 4644 | |
| 4645 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4646 | { |
| 4647 | ASSERT(!detail::coroHandle); |
| 4648 | detail::coroHandle = handle; |
| 4649 | } |
| 4650 | |
| 4651 | Nucleus::CoroutineHandle getHandleParam() |
| 4652 | { |
| 4653 | ASSERT(detail::coroHandle); |
| 4654 | auto handle = detail::coroHandle; |
| 4655 | detail::coroHandle = {}; |
| 4656 | return handle; |
| 4657 | } |
| 4658 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4659 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4660 | { |
| 4661 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4662 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4666 | { |
| 4667 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4668 | coroData->promisePtr = promisePtr; |
| 4669 | } |
| 4670 | |
| 4671 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4672 | { |
| 4673 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4674 | return coroData->promisePtr; |
| 4675 | } |
| 4676 | |
| 4677 | } // namespace coro |
| 4678 | } // namespace |
| 4679 | |
| 4680 | // Used to generate coroutines. |
| 4681 | // Lifetime: from yield to acquireCoroutine |
| 4682 | class CoroutineGenerator |
| 4683 | { |
| 4684 | public: |
| 4685 | CoroutineGenerator() |
| 4686 | { |
| 4687 | } |
| 4688 | |
| 4689 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4690 | void generateCoroutineBegin() |
| 4691 | { |
| 4692 | // Begin building the main coroutine_begin() function. |
| 4693 | // We insert these instructions at the top of the entry node, |
| 4694 | // before existing reactor-generated instructions. |
| 4695 | |
| 4696 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4697 | // { |
| 4698 | // this->handle = coro::getHandleParam(); |
| 4699 | // |
| 4700 | // YieldType promise; |
| 4701 | // coro::setPromisePtr(handle, &promise); // For await |
| 4702 | // |
| 4703 | // ... <REACTOR CODE> ... |
| 4704 | // |
| 4705 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4706 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4707 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4708 | |
| 4709 | // YieldType promise; |
| 4710 | // coro::setPromisePtr(handle, &promise); // For await |
| 4711 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4712 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4716 | void generateYield(Value *val) |
| 4717 | { |
| 4718 | // ... <REACTOR CODE> ... |
| 4719 | // |
| 4720 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4721 | // if (!coro::suspend(handle)) { |
| 4722 | // return false; // coroutine has been stopped by the caller. |
| 4723 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4724 | // |
| 4725 | // ... <REACTOR CODE> ... |
| 4726 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4727 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4728 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4729 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4730 | // if (!coro::suspend(handle)) { |
| 4731 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4732 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4733 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4734 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4735 | |
| 4736 | // return false; // coroutine has been stopped by the caller. |
| 4737 | ::basicBlock = doneBlock; |
| 4738 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4739 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4740 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4741 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4745 | |
| 4746 | // Generates the await function for the current coroutine. |
| 4747 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4748 | static FunctionUniquePtr generateAwaitFunction() |
| 4749 | { |
| 4750 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4751 | // { |
| 4752 | // if (coro::isDone()) |
| 4753 | // { |
| 4754 | // return false; |
| 4755 | // } |
| 4756 | // else // resume |
| 4757 | // { |
| 4758 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4759 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4760 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4761 | // return true; |
| 4762 | // } |
| 4763 | // } |
| 4764 | |
| 4765 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4766 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4767 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4768 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4769 | |
| 4770 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4771 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4772 | |
| 4773 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4774 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4775 | |
| 4776 | auto doneBlock = awaitFunc->makeNode(); |
| 4777 | { |
| 4778 | // return false; |
| 4779 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4780 | doneBlock->appendInst(ret); |
| 4781 | } |
| 4782 | |
| 4783 | auto resumeBlock = awaitFunc->makeNode(); |
| 4784 | { |
| 4785 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4786 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4787 | |
| 4788 | // *out = *promise; |
| 4789 | // Load promise value |
| 4790 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4791 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4792 | resumeBlock->appendInst(load); |
| 4793 | // Then store it in output param |
| 4794 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4795 | resumeBlock->appendInst(store); |
| 4796 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4797 | // coro::resume(handle); |
| 4798 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4799 | |
| 4800 | // return true; |
| 4801 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4802 | resumeBlock->appendInst(ret); |
| 4803 | } |
| 4804 | |
| 4805 | // if (coro::isDone()) |
| 4806 | // { |
| 4807 | // <doneBlock> |
| 4808 | // } |
| 4809 | // else // resume |
| 4810 | // { |
| 4811 | // <resumeBlock> |
| 4812 | // } |
| 4813 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4814 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4815 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4816 | bb->appendInst(br); |
| 4817 | |
| 4818 | return FunctionUniquePtr{ awaitFunc }; |
| 4819 | } |
| 4820 | |
| 4821 | // Generates the destroy function for the current coroutine. |
| 4822 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4823 | static FunctionUniquePtr generateDestroyFunction() |
| 4824 | { |
| 4825 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4826 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4827 | // 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] | 4828 | // return; |
| 4829 | // } |
| 4830 | |
| 4831 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4832 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4833 | |
| 4834 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4835 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4836 | |
| 4837 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4838 | |
| 4839 | auto *bb = destroyFunc->getEntryNode(); |
| 4840 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4841 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4842 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4843 | |
| 4844 | // return; |
| 4845 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4846 | bb->appendInst(ret); |
| 4847 | |
| 4848 | return FunctionUniquePtr{ destroyFunc }; |
| 4849 | } |
| 4850 | |
| 4851 | private: |
| 4852 | Ice::Variable *handle{}; |
| 4853 | Ice::Variable *promise{}; |
| 4854 | }; |
| 4855 | |
| 4856 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4857 | { |
| 4858 | // This doubles up as our coroutine handle |
| 4859 | auto coroData = coro::createCoroutineData(); |
| 4860 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4861 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4862 | if(coroData->useInternalScheduler) |
| 4863 | { |
| 4864 | ::getOrCreateScheduler().bind(); |
| 4865 | } |
| 4866 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4867 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4868 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4869 | // any fiber switch occurs. |
| 4870 | coro::setHandleParam(coroData); |
| 4871 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4872 | ASSERT(!coroData->routineFiber); |
| 4873 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4874 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4875 | beginFunc(); |
| 4876 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4877 | ASSERT(coroData->inRoutine); |
| 4878 | coroData->done = true; // coroutine is done. |
| 4879 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4880 | coroData->inRoutine = false; |
| 4881 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4882 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4883 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4884 | ASSERT(!coroData->mainFiber); |
| 4885 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4886 | |
| 4887 | // block until the first yield or coroutine end |
| 4888 | ASSERT(!coroData->inRoutine); |
| 4889 | coroData->inRoutine = true; |
| 4890 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4891 | while(coroData->inRoutine) |
| 4892 | { |
| 4893 | coroData->mainFiber->wait(); |
| 4894 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4895 | |
| 4896 | return coroData; |
| 4897 | } |
| 4898 | |
| 4899 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4900 | { |
| 4901 | // Start by creating a regular function |
| 4902 | createFunction(yieldType, params); |
| 4903 | |
| 4904 | // Save in case yield() is called |
| 4905 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4906 | ::coroYieldType = yieldType; |
| 4907 | } |
| 4908 | |
| 4909 | void Nucleus::yield(Value *val) |
| 4910 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4911 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4912 | Variable::materializeAll(); |
| 4913 | |
| 4914 | // On first yield, we start generating coroutine functions |
| 4915 | if(!::coroGen) |
| 4916 | { |
| 4917 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4918 | ::coroGen->generateCoroutineBegin(); |
| 4919 | } |
| 4920 | |
| 4921 | ASSERT(::coroGen); |
| 4922 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4923 | } |
| 4924 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4925 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4926 | { |
| 4927 | return false; |
| 4928 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4929 | |
| 4930 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4931 | { |
| 4932 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4933 | |
| 4934 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 4935 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4936 | if(::coroGen) |
| 4937 | { |
| 4938 | // Finish generating coroutine functions |
| 4939 | { |
| 4940 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4941 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4942 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4943 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4944 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4945 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4946 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4947 | // At this point, we no longer need the CoroutineGenerator. |
| 4948 | ::coroGen.reset(); |
| 4949 | ::coroYieldType = nullptr; |
| 4950 | |
| 4951 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4952 | { name, "await", "destroy" }, |
| 4953 | cfgEdit); |
| 4954 | |
| 4955 | return routine; |
| 4956 | } |
| 4957 | else |
| 4958 | { |
| 4959 | { |
| 4960 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4961 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | ::coroYieldType = nullptr; |
| 4965 | |
| 4966 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 4967 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 4968 | |
| 4969 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 4970 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 4971 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 4972 | return routine; |
| 4973 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4974 | } |
| 4975 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4976 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4977 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4978 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 4979 | |
| 4980 | if(isCoroutine) |
| 4981 | { |
| 4982 | return rr::invokeCoroutineBegin(func); |
| 4983 | } |
| 4984 | else |
| 4985 | { |
| 4986 | // For regular routines, just invoke the begin func directly |
| 4987 | return func(); |
| 4988 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4989 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4990 | |
| 4991 | } // namespace rr |