Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 15 | #include "Debug.hpp" |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 16 | #include "EmulatedIntrinsics.hpp" |
| 17 | #include "OptimalIntrinsics.hpp" |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 18 | #include "Print.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 19 | #include "Reactor.hpp" |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 20 | #include "ReactorDebugInfo.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 22 | #include "ExecutableMemory.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 23 | #include "Optimizer.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 25 | #include "src/IceCfg.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 26 | #include "src/IceCfgNode.h" |
| 27 | #include "src/IceELFObjectWriter.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 28 | #include "src/IceELFStreamer.h" |
| 29 | #include "src/IceGlobalContext.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 30 | #include "src/IceGlobalInits.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 31 | #include "src/IceTypes.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 32 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 34 | #include "llvm/Support/FileSystem.h" |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 36 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 37 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 38 | #include "marl/event.h" |
| 39 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 40 | #if __has_feature(memory_sanitizer) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 41 | # include <sanitizer/msan_interface.h> |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 42 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 43 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 44 | #if defined(_WIN32) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 45 | # ifndef WIN32_LEAN_AND_MEAN |
| 46 | # define WIN32_LEAN_AND_MEAN |
| 47 | # endif // !WIN32_LEAN_AND_MEAN |
| 48 | # ifndef NOMINMAX |
| 49 | # define NOMINMAX |
| 50 | # endif // !NOMINMAX |
| 51 | # include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 52 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 53 | |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 54 | #include <array> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 55 | #include <iostream> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 56 | #include <limits> |
| 57 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 58 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 59 | // Subzero utility functions |
| 60 | // These functions only accept and return Subzero (Ice) types, and do not access any globals. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 61 | namespace { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 62 | namespace sz { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 63 | |
| 64 | Ice::Cfg *createFunction(Ice::GlobalContext *context, Ice::Type returnType, const std::vector<Ice::Type> ¶mTypes) |
| 65 | { |
| 66 | uint32_t sequenceNumber = 0; |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame^] | 67 | auto *function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 68 | |
| 69 | function->setStackSizeLimit(512 * 1024); // 512 KiB |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 70 | |
| 71 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 72 | |
| 73 | for(auto type : paramTypes) |
| 74 | { |
| 75 | Ice::Variable *arg = function->makeVariable(type); |
| 76 | function->addArg(arg); |
| 77 | } |
| 78 | |
| 79 | Ice::CfgNode *node = function->makeNode(); |
| 80 | function->setEntryNode(node); |
| 81 | |
| 82 | return function; |
| 83 | } |
| 84 | |
| 85 | Ice::Type getPointerType(Ice::Type elementType) |
| 86 | { |
| 87 | if(sizeof(void *) == 8) |
| 88 | { |
| 89 | return Ice::IceType_i64; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | return Ice::IceType_i32; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 98 | { |
| 99 | int typeSize = Ice::typeWidthInBytes(type); |
| 100 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 101 | |
| 102 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 103 | auto address = function->makeVariable(getPointerType(type)); |
| 104 | auto alloca = Ice::InstAlloca::create(function, address, bytes, typeSize); |
| 105 | function->getEntryNode()->getInsts().push_front(alloca); |
| 106 | |
| 107 | return address; |
| 108 | } |
| 109 | |
| 110 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 111 | { |
| 112 | if(sizeof(void *) == 8) |
| 113 | { |
| 114 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 119 | } |
| 120 | } |
| 121 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 122 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 123 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 124 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 125 | // Wrapper for calls on C functions with Ice types |
| 126 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 127 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 128 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 129 | |
| 130 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 131 | // then truncate result to bool. |
| 132 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 133 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 134 | if(returningBool) |
| 135 | { |
| 136 | ret = function->makeVariable(Ice::IceType_i32); |
| 137 | } |
| 138 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 139 | { |
| 140 | ret = function->makeVariable(retTy); |
| 141 | } |
| 142 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 143 | auto call = Ice::InstCall::create(function, iceArgs.size(), ret, callTarget, false, false, isVariadic); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 144 | for(auto arg : iceArgs) |
| 145 | { |
| 146 | call->addArg(arg); |
| 147 | } |
| 148 | |
| 149 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 150 | |
| 151 | if(returningBool) |
| 152 | { |
| 153 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 154 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 155 | } |
| 156 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 157 | return ret; |
| 158 | } |
| 159 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 160 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 161 | { |
| 162 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 163 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 164 | } |
| 165 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 166 | // Wrapper for calls on C functions with Ice types |
| 167 | template<typename Return, typename... CArgs, typename... RArgs> |
| 168 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Return(fptr)(CArgs...), RArgs &&... args) |
| 169 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 170 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 171 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 172 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 173 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 174 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 177 | // Returns a non-const variable copy of const v |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 178 | Ice::Variable *createUnconstCast(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Constant *v) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 179 | { |
| 180 | Ice::Variable *result = function->makeVariable(v->getType()); |
| 181 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Bitcast, result, v); |
| 182 | basicBlock->appendInst(cast); |
| 183 | return result; |
| 184 | } |
| 185 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 186 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 187 | { |
| 188 | Ice::Variable *to = function->makeVariable(toType); |
| 189 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 190 | basicBlock->appendInst(cast); |
| 191 | return to; |
| 192 | } |
| 193 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 194 | Ice::Variable *createLoad(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *ptr, Ice::Type type, unsigned int align) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 195 | { |
| 196 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 197 | // absolute address. We circumvent this by casting to a non-const variable, and loading |
| 198 | // from that. |
| 199 | if(auto *cptr = llvm::dyn_cast<Ice::Constant>(ptr)) |
| 200 | { |
| 201 | ptr = sz::createUnconstCast(function, basicBlock, cptr); |
| 202 | } |
| 203 | |
| 204 | Ice::Variable *result = function->makeVariable(type); |
| 205 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 206 | basicBlock->appendInst(load); |
| 207 | |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 212 | } // namespace |
| 213 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 214 | namespace rr { |
| 215 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 216 | class CoroutineGenerator; |
| 217 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 218 | |
| 219 | namespace { |
| 220 | |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 221 | // Used to automatically invoke llvm_shutdown() when driver is unloaded |
| 222 | llvm::llvm_shutdown_obj llvmShutdownObj; |
| 223 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 224 | // Default configuration settings. Must be accessed under mutex lock. |
| 225 | std::mutex defaultConfigLock; |
| 226 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 227 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 228 | // This uses a static in a function to avoid the cost of a global static |
| 229 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 230 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 231 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 232 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 235 | Ice::GlobalContext *context = nullptr; |
| 236 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 237 | Ice::CfgNode *entryBlock = nullptr; |
| 238 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 239 | Ice::CfgNode *basicBlock = nullptr; |
| 240 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 241 | rr::ELFMemoryStreamer *routine = nullptr; |
| 242 | |
| 243 | std::mutex codegenMutex; |
| 244 | |
| 245 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 246 | Ice::Fdstream *out = nullptr; |
| 247 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 248 | // Coroutine globals |
| 249 | rr::Type *coroYieldType = nullptr; |
| 250 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 251 | marl::Scheduler &getOrCreateScheduler() |
| 252 | { |
| 253 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 254 | marl::Scheduler::Config cfg; |
| 255 | cfg.setWorkerThreadCount(8); |
| 256 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 257 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 258 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 259 | return *scheduler; |
| 260 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 261 | } // Anonymous namespace |
| 262 | |
| 263 | namespace { |
| 264 | |
| 265 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 266 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 267 | #endif |
| 268 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 269 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 270 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 271 | #endif |
| 272 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 273 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 274 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 275 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 276 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 277 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 278 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 279 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 280 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 281 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 282 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 283 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 284 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 287 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 288 | { |
| 289 | switch(memoryOrder) |
| 290 | { |
| 291 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 292 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 293 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 294 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 295 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 296 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
| 297 | } |
| 298 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 299 | } |
| 300 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 301 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 302 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 303 | public: |
| 304 | const static bool ARM; |
| 305 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 306 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 307 | private: |
| 308 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 309 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 310 | #if defined(__i386__) || defined(__x86_64__) |
| 311 | # if defined(_WIN32) |
| 312 | __cpuid(registers, info); |
| 313 | # else |
| 314 | __asm volatile("cpuid" |
| 315 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 316 | : "a"(info)); |
| 317 | # endif |
| 318 | #else |
| 319 | registers[0] = 0; |
| 320 | registers[1] = 0; |
| 321 | registers[2] = 0; |
| 322 | registers[3] = 0; |
| 323 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 326 | static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 327 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 328 | #if defined(__arm__) || defined(__aarch64__) |
| 329 | return true; |
| 330 | #elif defined(__i386__) || defined(__x86_64__) |
| 331 | return false; |
| 332 | #elif defined(__mips__) |
| 333 | return false; |
| 334 | #else |
| 335 | # error "Unknown architecture" |
| 336 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 337 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 338 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 339 | static bool detectSSE4_1() |
| 340 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 341 | #if defined(__i386__) || defined(__x86_64__) |
| 342 | int registers[4]; |
| 343 | cpuid(registers, 1); |
| 344 | return (registers[2] & 0x00080000) != 0; |
| 345 | #else |
| 346 | return false; |
| 347 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 348 | } |
| 349 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 350 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 351 | const bool CPUID::ARM = CPUID::detectARM(); |
| 352 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
| 353 | const bool emulateIntrinsics = false; |
| 354 | const bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 355 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 356 | constexpr bool subzeroDumpEnabled = false; |
| 357 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 358 | |
| 359 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 360 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 361 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 362 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 363 | |
| 364 | } // anonymous namespace |
| 365 | |
| 366 | namespace rr { |
| 367 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 368 | std::string BackendName() |
| 369 | { |
| 370 | return "Subzero"; |
| 371 | } |
| 372 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 373 | const Capabilities Caps = { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 374 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | enum EmulatedType |
| 378 | { |
| 379 | EmulatedShift = 16, |
| 380 | EmulatedV2 = 2 << EmulatedShift, |
| 381 | EmulatedV4 = 4 << EmulatedShift, |
| 382 | EmulatedV8 = 8 << EmulatedShift, |
| 383 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 384 | |
| 385 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 386 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 387 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 388 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 389 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 390 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 391 | }; |
| 392 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 393 | class Value : public Ice::Operand |
| 394 | {}; |
| 395 | class SwitchCases : public Ice::InstSwitch |
| 396 | {}; |
| 397 | class BasicBlock : public Ice::CfgNode |
| 398 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 399 | |
| 400 | Ice::Type T(Type *t) |
| 401 | { |
| 402 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 403 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 406 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 407 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 408 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | Type *T(EmulatedType t) |
| 412 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 413 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 416 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 417 | { |
| 418 | std::vector<Ice::Type> result; |
| 419 | result.reserve(types.size()); |
| 420 | for(auto &t : types) |
| 421 | { |
| 422 | result.push_back(T(t)); |
| 423 | } |
| 424 | return result; |
| 425 | } |
| 426 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 427 | Value *V(Ice::Operand *v) |
| 428 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 429 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 432 | Ice::Operand *V(Value *v) |
| 433 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 434 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 437 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 438 | { |
| 439 | std::vector<Ice::Operand *> result; |
| 440 | result.reserve(values.size()); |
| 441 | for(auto &v : values) |
| 442 | { |
| 443 | result.push_back(V(v)); |
| 444 | } |
| 445 | return result; |
| 446 | } |
| 447 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 448 | BasicBlock *B(Ice::CfgNode *b) |
| 449 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 450 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static size_t typeSize(Type *type) |
| 454 | { |
| 455 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 456 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 457 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 458 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 459 | case Type_v2i32: return 8; |
| 460 | case Type_v4i16: return 8; |
| 461 | case Type_v2i16: return 4; |
| 462 | case Type_v8i8: return 8; |
| 463 | case Type_v4i8: return 4; |
| 464 | case Type_v2f32: return 8; |
| 465 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | return Ice::typeWidthInBytes(T(type)); |
| 470 | } |
| 471 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 472 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 473 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 474 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 475 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 476 | { |
| 477 | Nucleus::createRetVoid(); |
| 478 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 479 | |
| 480 | // Connect the entry block to the top of the initial basic block |
| 481 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 482 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 483 | } |
| 484 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 485 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 486 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 487 | |
| 488 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 489 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 490 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 494 | { |
| 495 | return §ionHeader(elfHeader)[index]; |
| 496 | } |
| 497 | |
| 498 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 499 | { |
| 500 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 501 | |
| 502 | uint32_t index = relocation.getSymbol(); |
| 503 | int table = relocationTable.sh_link; |
| 504 | void *symbolValue = nullptr; |
| 505 | |
| 506 | if(index != SHN_UNDEF) |
| 507 | { |
| 508 | if(table == SHN_UNDEF) return nullptr; |
| 509 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 510 | |
| 511 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 512 | if(index >= symtab_entries) |
| 513 | { |
| 514 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 515 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 518 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 519 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 521 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 522 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 523 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 524 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 525 | symbolValue = reinterpret_cast<void *>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 526 | } |
| 527 | else |
| 528 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 529 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 530 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 533 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 534 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 535 | |
| 536 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 537 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 538 | switch(relocation.getType()) |
| 539 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 540 | case R_ARM_NONE: |
| 541 | // No relocation |
| 542 | break; |
| 543 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 544 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 545 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 546 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 547 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 548 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 549 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 550 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 551 | { |
| 552 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 553 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 554 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 555 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 556 | default: |
| 557 | ASSERT(false && "Unsupported relocation type"); |
| 558 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 559 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 560 | } |
| 561 | else |
| 562 | { |
| 563 | switch(relocation.getType()) |
| 564 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 565 | case R_386_NONE: |
| 566 | // No relocation |
| 567 | break; |
| 568 | case R_386_32: |
| 569 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 570 | break; |
| 571 | case R_386_PC32: |
| 572 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 573 | break; |
| 574 | default: |
| 575 | ASSERT(false && "Unsupported relocation type"); |
| 576 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 577 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 580 | return symbolValue; |
| 581 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 582 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 583 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 584 | { |
| 585 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 586 | |
| 587 | uint32_t index = relocation.getSymbol(); |
| 588 | int table = relocationTable.sh_link; |
| 589 | void *symbolValue = nullptr; |
| 590 | |
| 591 | if(index != SHN_UNDEF) |
| 592 | { |
| 593 | if(table == SHN_UNDEF) return nullptr; |
| 594 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 595 | |
| 596 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 597 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 598 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 599 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 600 | return nullptr; |
| 601 | } |
| 602 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 603 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 604 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 605 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 606 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 607 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 608 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 609 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 610 | symbolValue = reinterpret_cast<void *>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 611 | } |
| 612 | else |
| 613 | { |
| 614 | return nullptr; |
| 615 | } |
| 616 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 617 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 618 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 619 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 620 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 621 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 622 | switch(relocation.getType()) |
| 623 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 624 | case R_X86_64_NONE: |
| 625 | // No relocation |
| 626 | break; |
| 627 | case R_X86_64_64: |
| 628 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 629 | break; |
| 630 | case R_X86_64_PC32: |
| 631 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 632 | break; |
| 633 | case R_X86_64_32S: |
| 634 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 635 | break; |
| 636 | default: |
| 637 | ASSERT(false && "Unsupported relocation type"); |
| 638 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | return symbolValue; |
| 642 | } |
| 643 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 644 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 645 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 646 | const void *entry; |
| 647 | size_t codeSize = 0; |
| 648 | }; |
| 649 | |
| 650 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 651 | { |
| 652 | ASSERT(functionNames.size() > 0); |
| 653 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 654 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 655 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 656 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 657 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 658 | if(!elfHeader->checkMagic()) |
| 659 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 660 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 664 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 665 | #if defined(__i386__) |
| 666 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 667 | #elif defined(__x86_64__) |
| 668 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 669 | #elif defined(__arm__) |
| 670 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 671 | #elif defined(__aarch64__) |
| 672 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 673 | #elif defined(__mips__) |
| 674 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 675 | #else |
| 676 | # error "Unsupported platform" |
| 677 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 678 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 679 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 680 | |
| 681 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 682 | { |
| 683 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 684 | { |
| 685 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 686 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 687 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 688 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 689 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 690 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 691 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 692 | { |
| 693 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 694 | { |
| 695 | return j; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 700 | return static_cast<size_t>(-1); |
| 701 | }; |
| 702 | |
| 703 | size_t index = findSectionNameEntryIndex(); |
| 704 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 705 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 706 | } |
| 707 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 708 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 709 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 710 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 711 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 712 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 713 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 714 | const Elf32_Rel &relocation = ((const Elf32_Rel *)(elfImage + sectionHeader[i].sh_offset))[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 715 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 716 | } |
| 717 | } |
| 718 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 719 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 720 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 721 | |
| 722 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 723 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 724 | const Elf64_Rela &relocation = ((const Elf64_Rela *)(elfImage + sectionHeader[i].sh_offset))[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 725 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 730 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | template<typename T> |
| 734 | struct ExecutableAllocator |
| 735 | { |
| 736 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 737 | template<class U> |
| 738 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 739 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 740 | |
| 741 | using value_type = T; |
| 742 | using size_type = std::size_t; |
| 743 | |
| 744 | T *allocate(size_type n) |
| 745 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 746 | return (T *)allocateMemoryPages( |
| 747 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void deallocate(T *p, size_type n) |
| 751 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 752 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 753 | } |
| 754 | }; |
| 755 | |
| 756 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 757 | { |
| 758 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 759 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 760 | |
| 761 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 762 | ELFMemoryStreamer() |
| 763 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 764 | { |
| 765 | position = 0; |
| 766 | buffer.reserve(0x1000); |
| 767 | } |
| 768 | |
| 769 | ~ELFMemoryStreamer() override |
| 770 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void write8(uint8_t Value) override |
| 774 | { |
| 775 | if(position == (uint64_t)buffer.size()) |
| 776 | { |
| 777 | buffer.push_back(Value); |
| 778 | position++; |
| 779 | } |
| 780 | else if(position < (uint64_t)buffer.size()) |
| 781 | { |
| 782 | buffer[position] = Value; |
| 783 | position++; |
| 784 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 785 | else |
| 786 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void writeBytes(llvm::StringRef Bytes) override |
| 790 | { |
| 791 | std::size_t oldSize = buffer.size(); |
| 792 | buffer.resize(oldSize + Bytes.size()); |
| 793 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 794 | position += Bytes.size(); |
| 795 | } |
| 796 | |
| 797 | uint64_t tell() const override { return position; } |
| 798 | |
| 799 | void seek(uint64_t Off) override { position = Off; } |
| 800 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 801 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 802 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 803 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 804 | |
| 805 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 806 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 807 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 808 | for(auto &entryPoint : entryPoints) |
| 809 | { |
| 810 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 811 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 812 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 813 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 814 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 815 | } |
| 816 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 817 | void finalize() |
| 818 | { |
| 819 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 820 | |
| 821 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 822 | } |
| 823 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 824 | void setEntry(int index, const void *func) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 825 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 826 | ASSERT(func); |
| 827 | funcs[index] = func; |
| 828 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 829 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 830 | const void *getEntry(int index) const override |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 831 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 832 | ASSERT(funcs[index]); |
| 833 | return funcs[index]; |
| 834 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 835 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 836 | const void *addConstantData(const void *data, size_t size, size_t alignment = 1) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 837 | { |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 838 | // Check if we already have a suitable constant. |
| 839 | for(const auto &c : constantsPool) |
| 840 | { |
| 841 | void *ptr = c.data.get(); |
| 842 | size_t space = c.space; |
| 843 | |
| 844 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 845 | |
| 846 | if(space < size) |
| 847 | { |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | if(memcmp(data, alignedPtr, size) == 0) |
| 852 | { |
| 853 | return alignedPtr; |
| 854 | } |
| 855 | } |
| 856 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 857 | // TODO(b/148086935): Replace with a buffer allocator. |
| 858 | size_t space = size + alignment; |
| 859 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 860 | void *ptr = buf.get(); |
| 861 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 862 | ASSERT(alignedPtr); |
| 863 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 864 | constantsPool.emplace_back(std::move(buf), space); |
| 865 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 866 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 867 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 868 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 869 | private: |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 870 | struct Constant |
| 871 | { |
| 872 | Constant(std::unique_ptr<uint8_t[]> data, size_t space) |
| 873 | : data(std::move(data)) |
| 874 | , space(space) |
| 875 | {} |
| 876 | |
| 877 | std::unique_ptr<uint8_t[]> data; |
| 878 | size_t space; |
| 879 | }; |
| 880 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 881 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 882 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 883 | std::size_t position; |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 884 | std::vector<Constant> constantsPool; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 885 | }; |
| 886 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 887 | #ifdef ENABLE_RR_PRINT |
| 888 | void VPrintf(const std::vector<Value *> &vals) |
| 889 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 890 | sz::Call(::function, ::basicBlock, Ice::IceType_i32, reinterpret_cast<const void *>(rr::DebugPrintf), V(vals), true); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 891 | } |
| 892 | #endif // ENABLE_RR_PRINT |
| 893 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 894 | Nucleus::Nucleus() |
| 895 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 896 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 897 | |
| 898 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 899 | Ice::ClFlags::getParsedClFlags(Flags); |
| 900 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 901 | #if defined(__arm__) |
| 902 | Flags.setTargetArch(Ice::Target_ARM32); |
| 903 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 904 | #elif defined(__mips__) |
| 905 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 906 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 907 | #else // x86 |
| 908 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 909 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 910 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 911 | Flags.setOutFileType(Ice::FT_Elf); |
| 912 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
| 913 | Flags.setApplicationBinaryInterface(Ice::ABI_Platform); |
| 914 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 915 | Flags.setDisableHybridAssembly(true); |
| 916 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 917 | // Emit functions into separate sections in the ELF so we can find them by name |
| 918 | Flags.setFunctionSections(true); |
| 919 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 920 | static llvm::raw_os_ostream cout(std::cout); |
| 921 | static llvm::raw_os_ostream cerr(std::cerr); |
| 922 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 923 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 924 | { |
| 925 | // Decorate text asm with liveness info |
| 926 | Flags.setDecorateAsm(true); |
| 927 | } |
| 928 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 929 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 930 | { |
| 931 | std::error_code errorCode; |
| 932 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 933 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 934 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 935 | } |
| 936 | else |
| 937 | { |
| 938 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 939 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 940 | ::routine = elfMemory; |
| 941 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 942 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 943 | #if !__has_feature(memory_sanitizer) |
| 944 | // thread_local variables in shared libraries are initialized at load-time, |
| 945 | // but this is not observed by MemorySanitizer if the loader itself was not |
| 946 | // instrumented, leading to false-positive unitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 947 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 948 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 949 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | Nucleus::~Nucleus() |
| 953 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 954 | delete Variable::unmaterializedVariables; |
| 955 | Variable::unmaterializedVariables = nullptr; |
| 956 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 957 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 958 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 959 | |
| 960 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 961 | ::allocator = nullptr; |
| 962 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 963 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 964 | ::function = nullptr; |
| 965 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 966 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 967 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 968 | |
| 969 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 970 | ::elfFile = nullptr; |
| 971 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 972 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 973 | ::out = nullptr; |
| 974 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 975 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 976 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 977 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 978 | |
| 979 | ::codegenMutex.unlock(); |
| 980 | } |
| 981 | |
| 982 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 983 | { |
| 984 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 985 | ::defaultConfig() = cfg; |
| 986 | } |
| 987 | |
| 988 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 989 | { |
| 990 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 991 | auto &config = ::defaultConfig(); |
| 992 | config = cfgEdit.apply(config); |
| 993 | } |
| 994 | |
| 995 | Config Nucleus::getDefaultConfig() |
| 996 | { |
| 997 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 998 | return ::defaultConfig(); |
| 999 | } |
| 1000 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1001 | // This function lowers and produces executable binary code in memory for the input functions, |
| 1002 | // and returns a Routine with the entry points to these functions. |
| 1003 | template<size_t Count> |
| 1004 | static std::shared_ptr<Routine> acquireRoutine(Ice::Cfg *const (&functions)[Count], const char *const (&names)[Count], const Config::Edit &cfgEdit) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1005 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1006 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 1007 | // and GlobalContext::emitItems. |
| 1008 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1009 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1010 | { |
| 1011 | // Output dump strings immediately, rather than once buffer is full. Useful for debugging. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1012 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | ::context->emitFileHeader(); |
| 1016 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1017 | // Translate |
| 1018 | |
| 1019 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1020 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1021 | Ice::Cfg *currFunc = functions[i]; |
| 1022 | |
| 1023 | // Install function allocator in TLS for Cfg-specific container allocators |
| 1024 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 1025 | |
| 1026 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 1027 | |
| 1028 | rr::optimize(currFunc); |
| 1029 | |
| 1030 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1031 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1032 | |
| 1033 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1034 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1035 | |
| 1036 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1037 | |
| 1038 | if(subzeroEmitTextAsm) |
| 1039 | { |
| 1040 | currFunc->emit(); |
| 1041 | } |
| 1042 | |
| 1043 | currFunc->emitIAS(); |
Nicolas Capens | ff010f9 | 2021-02-01 12:22:53 -0500 | [diff] [blame^] | 1044 | |
| 1045 | if(currFunc->hasError()) |
| 1046 | { |
| 1047 | return nullptr; |
| 1048 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1049 | } |
| 1050 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1051 | // Emit items |
| 1052 | |
| 1053 | ::context->lowerGlobals(""); |
| 1054 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1055 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1056 | |
| 1057 | for(size_t i = 0; i < Count; ++i) |
| 1058 | { |
| 1059 | Ice::Cfg *currFunc = functions[i]; |
| 1060 | |
| 1061 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1062 | auto globals = currFunc->getGlobalInits(); |
| 1063 | if(globals && !globals->empty()) |
| 1064 | { |
| 1065 | ::context->getGlobals()->merge(globals.get()); |
| 1066 | } |
| 1067 | |
| 1068 | auto assembler = currFunc->releaseAssembler(); |
| 1069 | assembler->alignFunction(); |
| 1070 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1071 | } |
| 1072 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1073 | ::context->lowerGlobals("last"); |
| 1074 | ::context->lowerConstants(); |
| 1075 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1076 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1077 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1078 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1079 | objectWriter->writeNonUserSections(); |
| 1080 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1081 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1082 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1083 | ASSERT(entryPoints.size() == Count); |
| 1084 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1085 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1086 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1090 | |
| 1091 | Routine *handoffRoutine = ::routine; |
| 1092 | ::routine = nullptr; |
| 1093 | |
| 1094 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1095 | } |
| 1096 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1097 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 1098 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1099 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1100 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1101 | } |
| 1102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1103 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1104 | { |
| 1105 | Ice::Type type = T(t); |
| 1106 | int typeSize = Ice::typeWidthInBytes(type); |
| 1107 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1108 | |
| 1109 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1110 | auto address = ::function->makeVariable(T(getPointerType(t))); |
| 1111 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); |
| 1112 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1113 | |
| 1114 | return V(address); |
| 1115 | } |
| 1116 | |
| 1117 | BasicBlock *Nucleus::createBasicBlock() |
| 1118 | { |
| 1119 | return B(::function->makeNode()); |
| 1120 | } |
| 1121 | |
| 1122 | BasicBlock *Nucleus::getInsertBlock() |
| 1123 | { |
| 1124 | return B(::basicBlock); |
| 1125 | } |
| 1126 | |
| 1127 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1128 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1129 | // ASSERT(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1130 | |
| 1131 | Variable::materializeAll(); |
| 1132 | |
| 1133 | ::basicBlock = basicBlock; |
| 1134 | } |
| 1135 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1136 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1137 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1138 | ASSERT(::function == nullptr); |
| 1139 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1140 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1141 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1142 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1143 | |
| 1144 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1145 | |
| 1146 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1147 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1148 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1149 | // TODO: Get rid of this as a global, and create scoped allocs in every Nucleus function instead. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1150 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1151 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1152 | ::entryBlock = ::function->getEntryNode(); |
| 1153 | ::basicBlock = ::function->makeNode(); |
| 1154 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | Value *Nucleus::getArgument(unsigned int index) |
| 1158 | { |
| 1159 | return V(::function->getArgs()[index]); |
| 1160 | } |
| 1161 | |
| 1162 | void Nucleus::createRetVoid() |
| 1163 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1164 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1165 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1166 | // Code generated after this point is unreachable, so any variables |
| 1167 | // being read can safely return an undefined value. We have to avoid |
| 1168 | // materializing variables after the terminator ret instruction. |
| 1169 | Variable::killUnmaterialized(); |
| 1170 | |
| 1171 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1172 | ::basicBlock->appendInst(ret); |
| 1173 | } |
| 1174 | |
| 1175 | void Nucleus::createRet(Value *v) |
| 1176 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1177 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1178 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1179 | // Code generated after this point is unreachable, so any variables |
| 1180 | // being read can safely return an undefined value. We have to avoid |
| 1181 | // materializing variables after the terminator ret instruction. |
| 1182 | Variable::killUnmaterialized(); |
| 1183 | |
| 1184 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1185 | ::basicBlock->appendInst(ret); |
| 1186 | } |
| 1187 | |
| 1188 | void Nucleus::createBr(BasicBlock *dest) |
| 1189 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1190 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1191 | Variable::materializeAll(); |
| 1192 | |
| 1193 | auto br = Ice::InstBr::create(::function, dest); |
| 1194 | ::basicBlock->appendInst(br); |
| 1195 | } |
| 1196 | |
| 1197 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1198 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1199 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1200 | Variable::materializeAll(); |
| 1201 | |
| 1202 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1203 | ::basicBlock->appendInst(br); |
| 1204 | } |
| 1205 | |
| 1206 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1207 | { |
| 1208 | switch(op) |
| 1209 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1210 | case Ice::InstArithmetic::Add: |
| 1211 | case Ice::InstArithmetic::Fadd: |
| 1212 | case Ice::InstArithmetic::Mul: |
| 1213 | case Ice::InstArithmetic::Fmul: |
| 1214 | case Ice::InstArithmetic::And: |
| 1215 | case Ice::InstArithmetic::Or: |
| 1216 | case Ice::InstArithmetic::Xor: |
| 1217 | return true; |
| 1218 | default: |
| 1219 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1224 | { |
| 1225 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1226 | |
| 1227 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1228 | |
| 1229 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1230 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1231 | ::basicBlock->appendInst(arithmetic); |
| 1232 | |
| 1233 | return V(result); |
| 1234 | } |
| 1235 | |
| 1236 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1237 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1238 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1239 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
| 1240 | } |
| 1241 | |
| 1242 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1243 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1244 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1245 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
| 1246 | } |
| 1247 | |
| 1248 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1249 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1250 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1251 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
| 1252 | } |
| 1253 | |
| 1254 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1255 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1256 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1257 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
| 1258 | } |
| 1259 | |
| 1260 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1261 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1263 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
| 1264 | } |
| 1265 | |
| 1266 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1267 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1268 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1269 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
| 1270 | } |
| 1271 | |
| 1272 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1273 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1274 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1275 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
| 1276 | } |
| 1277 | |
| 1278 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1279 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1280 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1281 | return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs); |
| 1282 | } |
| 1283 | |
| 1284 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1285 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1286 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1287 | return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs); |
| 1288 | } |
| 1289 | |
| 1290 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1291 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1292 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1293 | return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs); |
| 1294 | } |
| 1295 | |
| 1296 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1297 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1299 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
| 1300 | } |
| 1301 | |
| 1302 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1305 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1306 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1307 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1308 | return nullptr; |
| 1309 | } |
| 1310 | |
| 1311 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1312 | { |
| 1313 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1317 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1318 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1319 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
| 1320 | } |
| 1321 | |
| 1322 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1323 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1324 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1325 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
| 1326 | } |
| 1327 | |
| 1328 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1329 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1330 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1331 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
| 1332 | } |
| 1333 | |
| 1334 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1335 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1337 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
| 1338 | } |
| 1339 | |
| 1340 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1343 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
| 1344 | } |
| 1345 | |
| 1346 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1347 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1348 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1349 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1350 | } |
| 1351 | |
| 1352 | Value *Nucleus::createNeg(Value *v) |
| 1353 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1355 | return createSub(createNullValue(T(v->getType())), v); |
| 1356 | } |
| 1357 | |
| 1358 | Value *Nucleus::createFNeg(Value *v) |
| 1359 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1361 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1362 | Value *negativeZero = Ice::isVectorType(v->getType()) ? createConstantVector(c, T(v->getType())) : V(::context->getConstantFloat(-0.0f)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1363 | |
| 1364 | return createFSub(negativeZero, v); |
| 1365 | } |
| 1366 | |
| 1367 | Value *Nucleus::createNot(Value *v) |
| 1368 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1369 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1370 | if(Ice::isScalarIntegerType(v->getType())) |
| 1371 | { |
| 1372 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1373 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1374 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1375 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1376 | int64_t c[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1377 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1378 | } |
| 1379 | } |
| 1380 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1381 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1382 | { |
| 1383 | #if defined(__i386__) || defined(__x86_64__) |
| 1384 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1385 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1386 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1387 | #else |
| 1388 | if(atomic) |
| 1389 | { |
| 1390 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1391 | } |
| 1392 | if(memoryOrder != std::memory_order_relaxed) |
| 1393 | { |
| 1394 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1395 | } |
| 1396 | #endif |
| 1397 | |
| 1398 | // Vulkan doesn't allow sequential memory order |
| 1399 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1400 | } |
| 1401 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1402 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1403 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1404 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1405 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1406 | |
| 1407 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1408 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1409 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1410 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1411 | { |
| 1412 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1413 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1414 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1415 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1417 | Int x = *Pointer<Int>(pointer); |
| 1418 | |
| 1419 | Int4 vector; |
| 1420 | vector = Insert(vector, x, 0); |
| 1421 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1422 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1423 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1424 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1425 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1426 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1427 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1428 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1429 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1430 | Int x = *Pointer<Int>(pointer); |
| 1431 | Int y = *Pointer<Int>(pointer + 4); |
| 1432 | |
| 1433 | Int4 vector; |
| 1434 | vector = Insert(vector, x, 0); |
| 1435 | vector = Insert(vector, y, 1); |
| 1436 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1437 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1438 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1439 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1440 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1441 | else |
| 1442 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1443 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1444 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1445 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1446 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1447 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1448 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1449 | auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1450 | load->addArg(ptr); |
| 1451 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1452 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1453 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1454 | } |
| 1455 | else |
| 1456 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1457 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1458 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1459 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1460 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1461 | return V(result); |
| 1462 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1463 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1464 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1465 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1466 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1467 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1468 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1469 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1470 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1471 | if(align != 0) |
| 1472 | { |
| 1473 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1474 | call->addArg(ptr); |
| 1475 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1476 | ::basicBlock->appendInst(call); |
| 1477 | } |
| 1478 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1479 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1480 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1481 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1482 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1483 | { |
| 1484 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1485 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1486 | if(typeSize(type) == 4) |
| 1487 | { |
| 1488 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1489 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1490 | ::basicBlock->appendInst(bitcast); |
| 1491 | |
| 1492 | RValue<Int4> v(V(vector)); |
| 1493 | |
| 1494 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1495 | Int x = Extract(v, 0); |
| 1496 | *Pointer<Int>(pointer) = x; |
| 1497 | } |
| 1498 | else if(typeSize(type) == 8) |
| 1499 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1500 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1501 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1502 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1503 | ::basicBlock->appendInst(bitcast); |
| 1504 | |
| 1505 | RValue<Int4> v(V(vector)); |
| 1506 | |
| 1507 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1508 | Int x = Extract(v, 0); |
| 1509 | *Pointer<Int>(pointer) = x; |
| 1510 | Int y = Extract(v, 1); |
| 1511 | *Pointer<Int>(pointer + 4) = y; |
| 1512 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1513 | else |
| 1514 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1515 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1516 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1517 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1518 | 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] | 1519 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1520 | auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic); |
| 1521 | store->addArg(value); |
| 1522 | store->addArg(ptr); |
| 1523 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1524 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1525 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1526 | } |
| 1527 | else |
| 1528 | { |
| 1529 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1530 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1531 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1532 | ::basicBlock->appendInst(store); |
| 1533 | } |
| 1534 | |
| 1535 | return value; |
| 1536 | } |
| 1537 | |
| 1538 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1539 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1540 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1541 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1542 | |
| 1543 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1544 | { |
| 1545 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1546 | |
| 1547 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1548 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1549 | return ptr; |
| 1550 | } |
| 1551 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1552 | return createAdd(ptr, createConstantInt(offset)); |
| 1553 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1554 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1555 | if(!Ice::isByteSizedType(T(type))) |
| 1556 | { |
| 1557 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1558 | } |
| 1559 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1560 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1561 | { |
| 1562 | if(unsignedIndex) |
| 1563 | { |
| 1564 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1565 | } |
| 1566 | else |
| 1567 | { |
| 1568 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | return createAdd(ptr, index); |
| 1573 | } |
| 1574 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1575 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1576 | { |
| 1577 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1578 | |
| 1579 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicRMW, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1580 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1581 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1582 | auto op = ::context->getConstantInt32(rmwOp); |
| 1583 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1584 | inst->addArg(op); |
| 1585 | inst->addArg(ptr); |
| 1586 | inst->addArg(value); |
| 1587 | inst->addArg(order); |
| 1588 | ::basicBlock->appendInst(inst); |
| 1589 | |
| 1590 | return V(result); |
| 1591 | } |
| 1592 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1593 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1594 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1595 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1596 | return createAtomicRMW(Ice::Intrinsics::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1600 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1601 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1602 | return createAtomicRMW(Ice::Intrinsics::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1606 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1607 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1608 | return createAtomicRMW(Ice::Intrinsics::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1612 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1613 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1614 | return createAtomicRMW(Ice::Intrinsics::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1618 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1619 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1620 | return createAtomicRMW(Ice::Intrinsics::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1624 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1625 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1626 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1630 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1631 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1632 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1633 | |
| 1634 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicCmpxchg, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1635 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1636 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1637 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1638 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1639 | inst->addArg(ptr); |
| 1640 | inst->addArg(compare); |
| 1641 | inst->addArg(value); |
| 1642 | inst->addArg(orderEq); |
| 1643 | inst->addArg(orderNeq); |
| 1644 | ::basicBlock->appendInst(inst); |
| 1645 | |
| 1646 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1650 | { |
| 1651 | if(v->getType() == T(destType)) |
| 1652 | { |
| 1653 | return v; |
| 1654 | } |
| 1655 | |
| 1656 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1657 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1658 | ::basicBlock->appendInst(cast); |
| 1659 | |
| 1660 | return V(result); |
| 1661 | } |
| 1662 | |
| 1663 | Value *Nucleus::createTrunc(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::Trunc, v, destType); |
| 1667 | } |
| 1668 | |
| 1669 | Value *Nucleus::createZExt(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 | return createCast(Ice::InstCast::Zext, v, destType); |
| 1673 | } |
| 1674 | |
| 1675 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1676 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1677 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1678 | return createCast(Ice::InstCast::Sext, v, destType); |
| 1679 | } |
| 1680 | |
| 1681 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1682 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1683 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1684 | return createCast(Ice::InstCast::Fptoui, v, destType); |
| 1685 | } |
| 1686 | |
| 1687 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1688 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1689 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1690 | return createCast(Ice::InstCast::Fptosi, v, destType); |
| 1691 | } |
| 1692 | |
| 1693 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1694 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1695 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1696 | return createCast(Ice::InstCast::Sitofp, v, destType); |
| 1697 | } |
| 1698 | |
| 1699 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1700 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1701 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1702 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
| 1703 | } |
| 1704 | |
| 1705 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 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 createCast(Ice::InstCast::Fpext, v, destType); |
| 1709 | } |
| 1710 | |
| 1711 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 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 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1715 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1716 | // emulate them by writing to the stack and reading back as the destination type. |
| 1717 | if(emulateMismatchedBitCast) |
| 1718 | { |
| 1719 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1720 | { |
| 1721 | Value *address = allocateStackVariable(destType); |
| 1722 | createStore(v, address, T(v->getType())); |
| 1723 | return createLoad(address, destType); |
| 1724 | } |
| 1725 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1726 | { |
| 1727 | Value *address = allocateStackVariable(T(v->getType())); |
| 1728 | createStore(v, address, T(v->getType())); |
| 1729 | return createLoad(address, destType); |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1734 | } |
| 1735 | |
| 1736 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1737 | { |
| 1738 | ASSERT(lhs->getType() == rhs->getType()); |
| 1739 | |
| 1740 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1741 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1742 | ::basicBlock->appendInst(cmp); |
| 1743 | |
| 1744 | return V(result); |
| 1745 | } |
| 1746 | |
| 1747 | Value *Nucleus::createPtrEQ(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::Eq, lhs, rhs); |
| 1751 | } |
| 1752 | |
| 1753 | Value *Nucleus::createICmpEQ(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::Eq, lhs, rhs); |
| 1757 | } |
| 1758 | |
| 1759 | Value *Nucleus::createICmpNE(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::Ne, lhs, rhs); |
| 1763 | } |
| 1764 | |
| 1765 | Value *Nucleus::createICmpUGT(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::Ugt, lhs, rhs); |
| 1769 | } |
| 1770 | |
| 1771 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1772 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1773 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1774 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1775 | } |
| 1776 | |
| 1777 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1778 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1779 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1780 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1781 | } |
| 1782 | |
| 1783 | Value *Nucleus::createICmpULE(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 createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1787 | } |
| 1788 | |
| 1789 | Value *Nucleus::createICmpSGT(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 createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1793 | } |
| 1794 | |
| 1795 | Value *Nucleus::createICmpSGE(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 createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1799 | } |
| 1800 | |
| 1801 | Value *Nucleus::createICmpSLT(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 createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1805 | } |
| 1806 | |
| 1807 | Value *Nucleus::createICmpSLE(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 createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1811 | } |
| 1812 | |
| 1813 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1814 | { |
| 1815 | ASSERT(lhs->getType() == rhs->getType()); |
| 1816 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1817 | |
| 1818 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1819 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1820 | ::basicBlock->appendInst(cmp); |
| 1821 | |
| 1822 | return V(result); |
| 1823 | } |
| 1824 | |
| 1825 | Value *Nucleus::createFCmpOEQ(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::Oeq, lhs, rhs); |
| 1829 | } |
| 1830 | |
| 1831 | Value *Nucleus::createFCmpOGT(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::Ogt, lhs, rhs); |
| 1835 | } |
| 1836 | |
| 1837 | Value *Nucleus::createFCmpOGE(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::Oge, lhs, rhs); |
| 1841 | } |
| 1842 | |
| 1843 | Value *Nucleus::createFCmpOLT(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::Olt, lhs, rhs); |
| 1847 | } |
| 1848 | |
| 1849 | Value *Nucleus::createFCmpOLE(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::Ole, lhs, rhs); |
| 1853 | } |
| 1854 | |
| 1855 | Value *Nucleus::createFCmpONE(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::One, lhs, rhs); |
| 1859 | } |
| 1860 | |
| 1861 | Value *Nucleus::createFCmpORD(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::Ord, lhs, rhs); |
| 1865 | } |
| 1866 | |
| 1867 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 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 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
| 1871 | } |
| 1872 | |
| 1873 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1874 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1875 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1876 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
| 1877 | } |
| 1878 | |
| 1879 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1880 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1881 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1882 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
| 1883 | } |
| 1884 | |
| 1885 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1886 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1887 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1888 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
| 1889 | } |
| 1890 | |
| 1891 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1892 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1893 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1894 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
| 1895 | } |
| 1896 | |
| 1897 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1898 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1899 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1900 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
| 1901 | } |
| 1902 | |
| 1903 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1904 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1905 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1906 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1907 | } |
| 1908 | |
| 1909 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1910 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1911 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1912 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1913 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1914 | ::basicBlock->appendInst(extract); |
| 1915 | |
| 1916 | return V(result); |
| 1917 | } |
| 1918 | |
| 1919 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1920 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1921 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1922 | auto result = ::function->makeVariable(vector->getType()); |
| 1923 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1924 | ::basicBlock->appendInst(insert); |
| 1925 | |
| 1926 | return V(result); |
| 1927 | } |
| 1928 | |
| 1929 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 1930 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1931 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1932 | ASSERT(V1->getType() == V2->getType()); |
| 1933 | |
| 1934 | int size = Ice::typeNumElements(V1->getType()); |
| 1935 | auto result = ::function->makeVariable(V1->getType()); |
| 1936 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1937 | |
| 1938 | for(int i = 0; i < size; i++) |
| 1939 | { |
| 1940 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1941 | } |
| 1942 | |
| 1943 | ::basicBlock->appendInst(shuffle); |
| 1944 | |
| 1945 | return V(result); |
| 1946 | } |
| 1947 | |
| 1948 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1949 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1950 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1951 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1952 | |
| 1953 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1954 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1955 | ::basicBlock->appendInst(select); |
| 1956 | |
| 1957 | return V(result); |
| 1958 | } |
| 1959 | |
| 1960 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1961 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1962 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1963 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1964 | ::basicBlock->appendInst(switchInst); |
| 1965 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1966 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1970 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1971 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1972 | switchCases->addBranch(label, label, branch); |
| 1973 | } |
| 1974 | |
| 1975 | void Nucleus::createUnreachable() |
| 1976 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1977 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1978 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1979 | ::basicBlock->appendInst(unreachable); |
| 1980 | } |
| 1981 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1982 | Type *Nucleus::getType(Value *value) |
| 1983 | { |
| 1984 | return T(V(value)->getType()); |
| 1985 | } |
| 1986 | |
| 1987 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1988 | { |
| 1989 | Ice::Type vecTy = T(vectorType); |
| 1990 | switch(vecTy) |
| 1991 | { |
| 1992 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1993 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1994 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1995 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1996 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1997 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1998 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1999 | default: |
| 2000 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 2001 | return {}; |
| 2002 | } |
| 2003 | } |
| 2004 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2005 | Type *Nucleus::getPointerType(Type *ElementType) |
| 2006 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 2007 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2008 | } |
| 2009 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2010 | static constexpr Ice::Type getNaturalIntType() |
| 2011 | { |
| 2012 | constexpr size_t intSize = sizeof(int); |
| 2013 | static_assert(intSize == 4 || intSize == 8, ""); |
| 2014 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 2015 | } |
| 2016 | |
| 2017 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 2018 | { |
| 2019 | Ice::Type valueTy = T(valueType); |
| 2020 | switch(valueTy) |
| 2021 | { |
| 2022 | case Ice::IceType_i32: |
| 2023 | return T(getNaturalIntType()); |
| 2024 | |
| 2025 | case Ice::IceType_f32: |
| 2026 | return T(Ice::IceType_f64); |
| 2027 | |
| 2028 | default: |
| 2029 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 2030 | return {}; |
| 2031 | } |
| 2032 | } |
| 2033 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2034 | Value *Nucleus::createNullValue(Type *Ty) |
| 2035 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2036 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2037 | if(Ice::isVectorType(T(Ty))) |
| 2038 | { |
| 2039 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2040 | 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] | 2041 | return createConstantVector(c, Ty); |
| 2042 | } |
| 2043 | else |
| 2044 | { |
| 2045 | return V(::context->getConstantZero(T(Ty))); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | Value *Nucleus::createConstantLong(int64_t 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->getConstantInt64(i)); |
| 2053 | } |
| 2054 | |
| 2055 | Value *Nucleus::createConstantInt(int i) |
| 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->getConstantInt32(i)); |
| 2059 | } |
| 2060 | |
| 2061 | Value *Nucleus::createConstantInt(unsigned int i) |
| 2062 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2063 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2064 | return V(::context->getConstantInt32(i)); |
| 2065 | } |
| 2066 | |
| 2067 | Value *Nucleus::createConstantBool(bool b) |
| 2068 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2069 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2070 | return V(::context->getConstantInt1(b)); |
| 2071 | } |
| 2072 | |
| 2073 | Value *Nucleus::createConstantByte(signed char i) |
| 2074 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2075 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2076 | return V(::context->getConstantInt8(i)); |
| 2077 | } |
| 2078 | |
| 2079 | Value *Nucleus::createConstantByte(unsigned char i) |
| 2080 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2081 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2082 | return V(::context->getConstantInt8(i)); |
| 2083 | } |
| 2084 | |
| 2085 | Value *Nucleus::createConstantShort(short i) |
| 2086 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2087 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2088 | return V(::context->getConstantInt16(i)); |
| 2089 | } |
| 2090 | |
| 2091 | Value *Nucleus::createConstantShort(unsigned short i) |
| 2092 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2093 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2094 | return V(::context->getConstantInt16(i)); |
| 2095 | } |
| 2096 | |
| 2097 | Value *Nucleus::createConstantFloat(float x) |
| 2098 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2099 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2100 | return V(::context->getConstantFloat(x)); |
| 2101 | } |
| 2102 | |
| 2103 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2104 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2105 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2106 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2107 | } |
| 2108 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2109 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2110 | { |
| 2111 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2112 | } |
| 2113 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2114 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2115 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2116 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2117 | const int vectorSize = 16; |
| 2118 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2119 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2120 | |
| 2121 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2122 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2123 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2124 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2125 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2126 | |
| 2127 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2128 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2129 | case Ice::IceType_v4i32: |
| 2130 | case Ice::IceType_v4i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2131 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2132 | 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] | 2133 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2134 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2135 | } |
| 2136 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2137 | case Ice::IceType_v4f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2138 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2139 | 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] | 2140 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2141 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2142 | } |
| 2143 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2144 | case Ice::IceType_v8i16: |
| 2145 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2146 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2147 | 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] | 2148 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2149 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2150 | } |
| 2151 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2152 | case Ice::IceType_v16i8: |
| 2153 | case Ice::IceType_v16i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2154 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2155 | 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] | 2156 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2157 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2158 | } |
| 2159 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2160 | case Type_v2i32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2161 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2162 | 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] | 2163 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2164 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2165 | } |
| 2166 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2167 | case Type_v2f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2168 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2169 | 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] | 2170 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2171 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2172 | } |
| 2173 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2174 | case Type_v4i16: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2175 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2176 | 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] | 2177 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2178 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2179 | } |
| 2180 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2181 | case Type_v8i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2182 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2183 | 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] | 2184 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2185 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2186 | } |
| 2187 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2188 | case Type_v4i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2189 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2190 | 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] | 2191 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2192 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2193 | } |
| 2194 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2195 | default: |
| 2196 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2197 | } |
| 2198 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2199 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2200 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2201 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2202 | return V(result); |
| 2203 | } |
| 2204 | |
| 2205 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2206 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2207 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2208 | } |
| 2209 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2210 | Value *Nucleus::createConstantString(const char *v) |
| 2211 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2212 | // 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] | 2213 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2214 | } |
| 2215 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2216 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2217 | { |
| 2218 | return T(Ice::IceType_void); |
| 2219 | } |
| 2220 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2221 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2222 | { |
| 2223 | return T(Ice::IceType_i1); |
| 2224 | } |
| 2225 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2226 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2227 | { |
| 2228 | return T(Ice::IceType_i8); |
| 2229 | } |
| 2230 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2231 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2232 | { |
| 2233 | return T(Ice::IceType_i8); |
| 2234 | } |
| 2235 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2236 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2237 | { |
| 2238 | return T(Ice::IceType_i16); |
| 2239 | } |
| 2240 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2241 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2242 | { |
| 2243 | return T(Ice::IceType_i16); |
| 2244 | } |
| 2245 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2246 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2247 | { |
| 2248 | return T(Type_v4i8); |
| 2249 | } |
| 2250 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2251 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2252 | { |
| 2253 | return T(Type_v4i8); |
| 2254 | } |
| 2255 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2256 | namespace { |
| 2257 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2258 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2259 | 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] | 2260 | } |
| 2261 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2262 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2263 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2264 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2268 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2269 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2270 | } |
| 2271 | } // namespace |
| 2272 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2273 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2274 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2275 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2276 | if(emulateIntrinsics) |
| 2277 | { |
| 2278 | Byte8 result; |
| 2279 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2280 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2281 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2282 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2283 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2284 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2285 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2286 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2287 | |
| 2288 | return result; |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2293 | 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] | 2294 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2295 | auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2296 | paddusb->addArg(x.value()); |
| 2297 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2298 | ::basicBlock->appendInst(paddusb); |
| 2299 | |
| 2300 | return RValue<Byte8>(V(result)); |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2305 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2306 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2307 | if(emulateIntrinsics) |
| 2308 | { |
| 2309 | Byte8 result; |
| 2310 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2311 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2312 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2313 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2314 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2315 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2316 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2317 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2318 | |
| 2319 | return result; |
| 2320 | } |
| 2321 | else |
| 2322 | { |
| 2323 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2324 | 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] | 2325 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2326 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2327 | psubusw->addArg(x.value()); |
| 2328 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2329 | ::basicBlock->appendInst(psubusw); |
| 2330 | |
| 2331 | return RValue<Byte8>(V(result)); |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 2336 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2337 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2338 | return RValue<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2342 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2343 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2344 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2348 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2349 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2350 | if(emulateIntrinsics) |
| 2351 | { |
| 2352 | SByte8 result; |
| 2353 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2354 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2355 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2356 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2357 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2358 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2359 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2360 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2361 | |
| 2362 | return result; |
| 2363 | } |
| 2364 | else |
| 2365 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2366 | #if defined(__i386__) || defined(__x86_64__) |
| 2367 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2368 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2369 | 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] | 2370 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2371 | return As<SByte8>(hi | lo); |
| 2372 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2373 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2374 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2375 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2376 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2377 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2378 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2379 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2380 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2381 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2382 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2383 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2384 | 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] | 2385 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2386 | else |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2387 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2388 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2389 | 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] | 2390 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2391 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2392 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2393 | ::basicBlock->appendInst(movmsk); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2394 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2395 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2396 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2397 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2398 | |
| 2399 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2400 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2401 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2402 | // } |
| 2403 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2404 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2405 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2406 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2407 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2408 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2409 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2410 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2411 | { |
| 2412 | return T(Type_v8i8); |
| 2413 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2414 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2415 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2416 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2417 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2418 | // } |
| 2419 | |
| 2420 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2421 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2422 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2423 | // } |
| 2424 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2425 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 2426 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2427 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2428 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2429 | } |
| 2430 | |
| 2431 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2432 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2433 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2434 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2435 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2436 | SByte8 result; |
| 2437 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2438 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2439 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2440 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2441 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2442 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2443 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2444 | 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] | 2445 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2446 | return result; |
| 2447 | } |
| 2448 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2449 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2450 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2451 | 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] | 2452 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2453 | auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2454 | paddsb->addArg(x.value()); |
| 2455 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2456 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2457 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2458 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2459 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2460 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2461 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2462 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2463 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2464 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2465 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2466 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2467 | SByte8 result; |
| 2468 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2469 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2470 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2471 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2472 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2473 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2474 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2475 | 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] | 2476 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2477 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2478 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2479 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2480 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2481 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2482 | 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] | 2483 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2484 | auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2485 | psubsb->addArg(x.value()); |
| 2486 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2487 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2488 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2489 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2490 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2491 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2492 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2493 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2494 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2495 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2496 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2497 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2498 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2499 | 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] | 2500 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2501 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2502 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2503 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2504 | 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] | 2505 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2506 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2507 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2508 | ::basicBlock->appendInst(movmsk); |
| 2509 | |
| 2510 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2511 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2512 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2513 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2514 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 2515 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2516 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2517 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2518 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2519 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2520 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2521 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2522 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2523 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2524 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2525 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2526 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2527 | { |
| 2528 | return T(Type_v8i8); |
| 2529 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2530 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2531 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2532 | { |
| 2533 | return T(Ice::IceType_v16i8); |
| 2534 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2535 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2536 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2537 | { |
| 2538 | return T(Ice::IceType_v16i8); |
| 2539 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2540 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2541 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2542 | { |
| 2543 | return T(Type_v2i16); |
| 2544 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2545 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2546 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2547 | { |
| 2548 | return T(Type_v2i16); |
| 2549 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2550 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2551 | Short4::Short4(RValue<Int4> cast) |
| 2552 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2553 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2554 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2555 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2556 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2557 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2558 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2559 | |
| 2560 | storeValue(short4); |
| 2561 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2562 | |
| 2563 | // Short4::Short4(RValue<Float> cast) |
| 2564 | // { |
| 2565 | // } |
| 2566 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2567 | Short4::Short4(RValue<Float4> cast) |
| 2568 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2569 | // TODO(b/150791192): Generalize and optimize |
| 2570 | auto smin = std::numeric_limits<short>::min(); |
| 2571 | auto smax = std::numeric_limits<short>::max(); |
| 2572 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2576 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2577 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2578 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2579 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2580 | Short4 result; |
| 2581 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2582 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2583 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2584 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2585 | |
| 2586 | return result; |
| 2587 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2588 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2589 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2590 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2591 | } |
| 2592 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2593 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2594 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2595 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2596 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2597 | if(emulateIntrinsics) |
| 2598 | { |
| 2599 | Short4 result; |
| 2600 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2601 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2602 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2603 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2604 | |
| 2605 | return result; |
| 2606 | } |
| 2607 | else |
| 2608 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2609 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2614 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2615 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2616 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2617 | 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] | 2618 | ::basicBlock->appendInst(cmp); |
| 2619 | |
| 2620 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2621 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2622 | ::basicBlock->appendInst(select); |
| 2623 | |
| 2624 | return RValue<Short4>(V(result)); |
| 2625 | } |
| 2626 | |
| 2627 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2628 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2629 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2630 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2631 | 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] | 2632 | ::basicBlock->appendInst(cmp); |
| 2633 | |
| 2634 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2635 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2636 | ::basicBlock->appendInst(select); |
| 2637 | |
| 2638 | return RValue<Short4>(V(result)); |
| 2639 | } |
| 2640 | |
| 2641 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 2642 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2643 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2644 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2645 | } |
| 2646 | |
| 2647 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2648 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2649 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2650 | if(emulateIntrinsics) |
| 2651 | { |
| 2652 | Short4 result; |
| 2653 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2654 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2655 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2656 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2657 | |
| 2658 | return result; |
| 2659 | } |
| 2660 | else |
| 2661 | { |
| 2662 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2663 | 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] | 2664 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2665 | auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2666 | paddsw->addArg(x.value()); |
| 2667 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2668 | ::basicBlock->appendInst(paddsw); |
| 2669 | |
| 2670 | return RValue<Short4>(V(result)); |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2675 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2676 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2677 | if(emulateIntrinsics) |
| 2678 | { |
| 2679 | Short4 result; |
| 2680 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2681 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2682 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2683 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2684 | |
| 2685 | return result; |
| 2686 | } |
| 2687 | else |
| 2688 | { |
| 2689 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2690 | 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] | 2691 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2692 | auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2693 | psubsw->addArg(x.value()); |
| 2694 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2695 | ::basicBlock->appendInst(psubsw); |
| 2696 | |
| 2697 | return RValue<Short4>(V(result)); |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2702 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2703 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2704 | if(emulateIntrinsics) |
| 2705 | { |
| 2706 | Short4 result; |
| 2707 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2708 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2709 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2710 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2711 | |
| 2712 | return result; |
| 2713 | } |
| 2714 | else |
| 2715 | { |
| 2716 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2717 | 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] | 2718 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2719 | auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2720 | pmulhw->addArg(x.value()); |
| 2721 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2722 | ::basicBlock->appendInst(pmulhw); |
| 2723 | |
| 2724 | return RValue<Short4>(V(result)); |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2729 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2730 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2731 | if(emulateIntrinsics) |
| 2732 | { |
| 2733 | Int2 result; |
| 2734 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2735 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2736 | |
| 2737 | return result; |
| 2738 | } |
| 2739 | else |
| 2740 | { |
| 2741 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2742 | 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] | 2743 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2744 | auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2745 | pmaddwd->addArg(x.value()); |
| 2746 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2747 | ::basicBlock->appendInst(pmaddwd); |
| 2748 | |
| 2749 | return As<Int2>(V(result)); |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2754 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2755 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2756 | if(emulateIntrinsics) |
| 2757 | { |
| 2758 | SByte8 result; |
| 2759 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2760 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2761 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2762 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2763 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2764 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2765 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2766 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2767 | |
| 2768 | return result; |
| 2769 | } |
| 2770 | else |
| 2771 | { |
| 2772 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2773 | 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] | 2774 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2775 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2776 | pack->addArg(x.value()); |
| 2777 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2778 | ::basicBlock->appendInst(pack); |
| 2779 | |
| 2780 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2785 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2786 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2787 | if(emulateIntrinsics) |
| 2788 | { |
| 2789 | Byte8 result; |
| 2790 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2791 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2792 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2793 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2794 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2795 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2796 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2797 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2798 | |
| 2799 | return result; |
| 2800 | } |
| 2801 | else |
| 2802 | { |
| 2803 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2804 | 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] | 2805 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2806 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2807 | pack->addArg(x.value()); |
| 2808 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2809 | ::basicBlock->appendInst(pack); |
| 2810 | |
| 2811 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2816 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2817 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2818 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2822 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2823 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2824 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2825 | } |
| 2826 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2827 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2828 | { |
| 2829 | return T(Type_v4i16); |
| 2830 | } |
| 2831 | |
| 2832 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2833 | { |
| 2834 | if(saturate) |
| 2835 | { |
| 2836 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2837 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2838 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2839 | // PackUnsigned takes care of 0x0000 saturation. |
| 2840 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2841 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2842 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2843 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2844 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2845 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2846 | Int4 int4(cast); |
| 2847 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2848 | } |
| 2849 | else |
| 2850 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2851 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2852 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2853 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2854 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2855 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2856 | *this = Short4(Int4(cast)); |
| 2857 | } |
| 2858 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2859 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2860 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2861 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2862 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2866 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2867 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2871 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2872 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2873 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2874 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2875 | { |
| 2876 | UShort4 result; |
| 2877 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2878 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2879 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2880 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2881 | |
| 2882 | return result; |
| 2883 | } |
| 2884 | else |
| 2885 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2886 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2891 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2892 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2893 | if(emulateIntrinsics) |
| 2894 | { |
| 2895 | UShort4 result; |
| 2896 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2897 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2898 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2899 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2900 | |
| 2901 | return result; |
| 2902 | } |
| 2903 | else |
| 2904 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2905 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2910 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2911 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2912 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2913 | 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] | 2914 | ::basicBlock->appendInst(cmp); |
| 2915 | |
| 2916 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2917 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2918 | ::basicBlock->appendInst(select); |
| 2919 | |
| 2920 | return RValue<UShort4>(V(result)); |
| 2921 | } |
| 2922 | |
| 2923 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2924 | { |
| 2925 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2926 | 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] | 2927 | ::basicBlock->appendInst(cmp); |
| 2928 | |
| 2929 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2930 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2931 | ::basicBlock->appendInst(select); |
| 2932 | |
| 2933 | return RValue<UShort4>(V(result)); |
| 2934 | } |
| 2935 | |
| 2936 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 2937 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2938 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2939 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2940 | } |
| 2941 | |
| 2942 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2943 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2944 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2945 | if(emulateIntrinsics) |
| 2946 | { |
| 2947 | UShort4 result; |
| 2948 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2949 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2950 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2951 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2952 | |
| 2953 | return result; |
| 2954 | } |
| 2955 | else |
| 2956 | { |
| 2957 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2958 | 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] | 2959 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2960 | auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2961 | paddusw->addArg(x.value()); |
| 2962 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2963 | ::basicBlock->appendInst(paddusw); |
| 2964 | |
| 2965 | return RValue<UShort4>(V(result)); |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2970 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2971 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2972 | if(emulateIntrinsics) |
| 2973 | { |
| 2974 | UShort4 result; |
| 2975 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2976 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2977 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2978 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2979 | |
| 2980 | return result; |
| 2981 | } |
| 2982 | else |
| 2983 | { |
| 2984 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2985 | 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] | 2986 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2987 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2988 | psubusw->addArg(x.value()); |
| 2989 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2990 | ::basicBlock->appendInst(psubusw); |
| 2991 | |
| 2992 | return RValue<UShort4>(V(result)); |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> 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 | if(emulateIntrinsics) |
| 3000 | { |
| 3001 | UShort4 result; |
| 3002 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 3003 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 3004 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 3005 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 3006 | |
| 3007 | return result; |
| 3008 | } |
| 3009 | else |
| 3010 | { |
| 3011 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3012 | 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] | 3013 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3014 | auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3015 | pmulhuw->addArg(x.value()); |
| 3016 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3017 | ::basicBlock->appendInst(pmulhuw); |
| 3018 | |
| 3019 | return RValue<UShort4>(V(result)); |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3024 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3025 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3026 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3027 | |
| 3028 | // Scalarized implementation. |
| 3029 | Int4 result; |
| 3030 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 3031 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 3032 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 3033 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 3034 | |
| 3035 | return result; |
| 3036 | } |
| 3037 | |
| 3038 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3039 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3040 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3041 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3042 | |
| 3043 | if(false) // Partial product based implementation. |
| 3044 | { |
| 3045 | auto xh = x >> 16; |
| 3046 | auto yh = y >> 16; |
| 3047 | auto xl = x & UInt4(0x0000FFFF); |
| 3048 | auto yl = y & UInt4(0x0000FFFF); |
| 3049 | auto xlyh = xl * yh; |
| 3050 | auto xhyl = xh * yl; |
| 3051 | auto xlyhh = xlyh >> 16; |
| 3052 | auto xhylh = xhyl >> 16; |
| 3053 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3054 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3055 | auto xlylh = (xl * yl) >> 16; |
| 3056 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3057 | |
| 3058 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3059 | } |
| 3060 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3061 | // Scalarized implementation. |
| 3062 | Int4 result; |
| 3063 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3064 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3065 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3066 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3067 | |
| 3068 | return As<UInt4>(result); |
| 3069 | } |
| 3070 | |
| 3071 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3072 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3073 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3074 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3075 | return UShort4(0); |
| 3076 | } |
| 3077 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3078 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3079 | { |
| 3080 | return T(Type_v4i16); |
| 3081 | } |
| 3082 | |
| 3083 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 3084 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3085 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3086 | return RValue<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3090 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3091 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3092 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3096 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3097 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3098 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3099 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3100 | Short8 result; |
| 3101 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3102 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3103 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3104 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3105 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3106 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3107 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3108 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3109 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3110 | return result; |
| 3111 | } |
| 3112 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3113 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3114 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3115 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3116 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3117 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3118 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 3119 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3120 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3121 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3122 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3123 | Short8 result; |
| 3124 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 3125 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 3126 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 3127 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 3128 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 3129 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 3130 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 3131 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3132 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3133 | return result; |
| 3134 | } |
| 3135 | else |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3136 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3137 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3138 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3139 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3140 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3141 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3142 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3143 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3144 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3145 | return Int4(0); |
| 3146 | } |
| 3147 | |
| 3148 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 3149 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3150 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3151 | UNIMPLEMENTED_NO_BUG("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3152 | return Short8(0); |
| 3153 | } |
| 3154 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3155 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3156 | { |
| 3157 | return T(Ice::IceType_v8i16); |
| 3158 | } |
| 3159 | |
| 3160 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 3161 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3162 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3163 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3167 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3168 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3169 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3173 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3174 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3175 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3176 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3177 | UShort8 result; |
| 3178 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3179 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3180 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3181 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3182 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3183 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3184 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3185 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3186 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3187 | return result; |
| 3188 | } |
| 3189 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3190 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3191 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3192 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3193 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3194 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3195 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 3196 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3197 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3198 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3199 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3200 | UShort8 result; |
| 3201 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 3202 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 3203 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 3204 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 3205 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 3206 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 3207 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 3208 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3209 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3210 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3211 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3212 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3213 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3214 | return RValue<UShort8>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3215 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3216 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3217 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3218 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3219 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3220 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3221 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3222 | return UShort8(0); |
| 3223 | } |
| 3224 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3225 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3226 | { |
| 3227 | return T(Ice::IceType_v8i16); |
| 3228 | } |
| 3229 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3230 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3231 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3232 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3233 | RValue<Int> res = val; |
| 3234 | val += 1; |
| 3235 | return res; |
| 3236 | } |
| 3237 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3238 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3239 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3240 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3241 | val += 1; |
| 3242 | return val; |
| 3243 | } |
| 3244 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3245 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3246 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3247 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3248 | RValue<Int> res = val; |
| 3249 | val -= 1; |
| 3250 | return res; |
| 3251 | } |
| 3252 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3253 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3254 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3255 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3256 | val -= 1; |
| 3257 | return val; |
| 3258 | } |
| 3259 | |
| 3260 | RValue<Int> RoundInt(RValue<Float> cast) |
| 3261 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3263 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3264 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3265 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3266 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3267 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3268 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3269 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3270 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3271 | 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] | 3272 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3273 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3274 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3275 | ::basicBlock->appendInst(nearbyint); |
| 3276 | |
| 3277 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3278 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3279 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3280 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3281 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3282 | { |
| 3283 | return T(Ice::IceType_i32); |
| 3284 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3285 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3286 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3287 | { |
| 3288 | return T(Ice::IceType_i64); |
| 3289 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3290 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3291 | UInt::UInt(RValue<Float> cast) |
| 3292 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3293 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3294 | // Smallest positive value representable in UInt, but not in Int |
| 3295 | const unsigned int ustart = 0x80000000u; |
| 3296 | const float ustartf = float(ustart); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3297 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3298 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3299 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3300 | // Check if the value can be represented as an Int |
| 3301 | IfThenElse(cast >= ustartf, |
| 3302 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3303 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3304 | // Otherwise, just convert normally |
| 3305 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3306 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3307 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3308 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3309 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3310 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3311 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3312 | RValue<UInt> res = val; |
| 3313 | val += 1; |
| 3314 | return res; |
| 3315 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3316 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3317 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3318 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3319 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3320 | val += 1; |
| 3321 | return val; |
| 3322 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3323 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3324 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3325 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3326 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3327 | RValue<UInt> res = val; |
| 3328 | val -= 1; |
| 3329 | return res; |
| 3330 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3331 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3332 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3333 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3334 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3335 | val -= 1; |
| 3336 | return val; |
| 3337 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3338 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3339 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3340 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3341 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3342 | // } |
| 3343 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3344 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3345 | { |
| 3346 | return T(Ice::IceType_i32); |
| 3347 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3348 | |
| 3349 | // Int2::Int2(RValue<Int> cast) |
| 3350 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3351 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3352 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3353 | // |
| 3354 | // Constant *shuffle[2]; |
| 3355 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3356 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3357 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3358 | // 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] | 3359 | // |
| 3360 | // storeValue(replicate); |
| 3361 | // } |
| 3362 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3363 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3364 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3365 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3366 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3367 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3368 | Int2 result; |
| 3369 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3370 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3371 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3372 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3373 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3374 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3375 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3376 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3377 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3378 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3379 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3380 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3381 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3382 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3383 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3384 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3385 | Int2 result; |
| 3386 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3387 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3388 | |
| 3389 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3390 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3391 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3392 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3393 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3394 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3395 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3396 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3397 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3398 | { |
| 3399 | return T(Type_v2i32); |
| 3400 | } |
| 3401 | |
| 3402 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 3403 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3404 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3405 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3406 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3407 | UInt2 result; |
| 3408 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3409 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3410 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3411 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3412 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3413 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3414 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3415 | return RValue<UInt2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3416 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3417 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3418 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3419 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3420 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3421 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3422 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3423 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3424 | UInt2 result; |
| 3425 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3426 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3427 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3428 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3429 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3430 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3431 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3432 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3433 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3434 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3435 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3436 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3437 | { |
| 3438 | return T(Type_v2i32); |
| 3439 | } |
| 3440 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3441 | Int4::Int4(RValue<Byte4> cast) |
| 3442 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3445 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3446 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3447 | |
| 3448 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3449 | 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] | 3450 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3451 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3452 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3453 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3454 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3455 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3456 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3457 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3458 | storeValue(f); |
| 3459 | } |
| 3460 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3461 | Int4::Int4(RValue<SByte4> cast) |
| 3462 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3463 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3464 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3465 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3466 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3467 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3468 | 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] | 3469 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3470 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3471 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3472 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3473 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3474 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3475 | |
| 3476 | *this = As<Int4>(e) >> 24; |
| 3477 | } |
| 3478 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3479 | Int4::Int4(RValue<Short4> cast) |
| 3480 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3481 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3482 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3483 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3484 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3485 | |
| 3486 | *this = As<Int4>(c) >> 16; |
| 3487 | } |
| 3488 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3489 | Int4::Int4(RValue<UShort4> cast) |
| 3490 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3491 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3492 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3493 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3494 | 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] | 3495 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3496 | storeValue(d); |
| 3497 | } |
| 3498 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3499 | Int4::Int4(RValue<Int> rhs) |
| 3500 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3501 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3502 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3503 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3504 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3505 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3506 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3507 | |
| 3508 | storeValue(replicate); |
| 3509 | } |
| 3510 | |
| 3511 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3512 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3513 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3514 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3515 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3516 | Int4 result; |
| 3517 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3518 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3519 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3520 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3521 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3522 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3523 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3524 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3525 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3526 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3527 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3528 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3529 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3530 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3531 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3532 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3533 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3534 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3535 | Int4 result; |
| 3536 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3537 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3538 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3539 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3540 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3541 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3542 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3543 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3544 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3545 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3546 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3547 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3548 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3549 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3550 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3551 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3552 | return RValue<Int4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3556 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3557 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3558 | return RValue<Int4>(Nucleus::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3562 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3563 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3564 | return RValue<Int4>(Nucleus::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3568 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3569 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3570 | return RValue<Int4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3574 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3575 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3576 | return RValue<Int4>(Nucleus::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3580 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3581 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3582 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3586 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3587 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3588 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3589 | 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] | 3590 | ::basicBlock->appendInst(cmp); |
| 3591 | |
| 3592 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3593 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3594 | ::basicBlock->appendInst(select); |
| 3595 | |
| 3596 | return RValue<Int4>(V(result)); |
| 3597 | } |
| 3598 | |
| 3599 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3600 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3601 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3602 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3603 | 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] | 3604 | ::basicBlock->appendInst(cmp); |
| 3605 | |
| 3606 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3607 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3608 | ::basicBlock->appendInst(select); |
| 3609 | |
| 3610 | return RValue<Int4>(V(result)); |
| 3611 | } |
| 3612 | |
| 3613 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3614 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3615 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3616 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3617 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3618 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3619 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3620 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3621 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3622 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3623 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3624 | 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] | 3625 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3626 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3627 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3628 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3629 | |
| 3630 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3631 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3632 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3633 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3634 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3635 | { |
| 3636 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3637 | |
| 3638 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3639 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3640 | // saturate to 0x80000000. |
| 3641 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3642 | |
| 3643 | if(emulateIntrinsics || CPUID::ARM) |
| 3644 | { |
| 3645 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3646 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3647 | } |
| 3648 | else |
| 3649 | { |
| 3650 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3651 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 3652 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3653 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3654 | nearbyint->addArg(clamped.value()); |
| 3655 | ::basicBlock->appendInst(nearbyint); |
| 3656 | |
| 3657 | return RValue<Int4>(V(result)); |
| 3658 | } |
| 3659 | } |
| 3660 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3661 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3662 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3663 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3664 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3665 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3666 | Short8 result; |
| 3667 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3668 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3669 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3670 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3671 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3672 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3673 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3674 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3675 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3676 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3677 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3678 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3679 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3680 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3681 | 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] | 3682 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3683 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3684 | pack->addArg(x.value()); |
| 3685 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3686 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3687 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3688 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3689 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3690 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3691 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3692 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3693 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3694 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3695 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3696 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3697 | RValue<Int4> sx = As<Int4>(x); |
| 3698 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3699 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3700 | RValue<Int4> sy = As<Int4>(y); |
| 3701 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3702 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3703 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3704 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3705 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3706 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3707 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3708 | 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] | 3709 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3710 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3711 | pack->addArg(x.value()); |
| 3712 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3713 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3714 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3715 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3716 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3717 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3718 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3719 | RValue<Int> SignMask(RValue<Int4> x) |
| 3720 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3721 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3722 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3723 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3724 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3725 | 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] | 3726 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3727 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3728 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3729 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3730 | 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] | 3731 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3732 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3733 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3734 | ::basicBlock->appendInst(movmsk); |
| 3735 | |
| 3736 | return RValue<Int>(V(result)); |
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 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3739 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3740 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3741 | { |
| 3742 | return T(Ice::IceType_v4i32); |
| 3743 | } |
| 3744 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3745 | UInt4::UInt4(RValue<Float4> cast) |
| 3746 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3747 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3748 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3749 | // Smallest positive value representable in UInt, but not in Int |
| 3750 | const unsigned int ustart = 0x80000000u; |
| 3751 | const float ustartf = float(ustart); |
| 3752 | |
| 3753 | // Check if the value can be represented as an Int |
| 3754 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3755 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3756 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3757 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3758 | (~uiValue & Int4(cast)); |
| 3759 | // 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] | 3760 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3761 | } |
| 3762 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3763 | UInt4::UInt4(RValue<UInt> rhs) |
| 3764 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3765 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3766 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3767 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3768 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3769 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3770 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3771 | |
| 3772 | storeValue(replicate); |
| 3773 | } |
| 3774 | |
| 3775 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3776 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3777 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3778 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3779 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3780 | UInt4 result; |
| 3781 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3782 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3783 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3784 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3785 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3786 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3787 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3788 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3789 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3790 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3791 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3792 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3793 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3794 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3795 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3796 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3797 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3798 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3799 | UInt4 result; |
| 3800 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3801 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3802 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3803 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3804 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3805 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3806 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3807 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3808 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3809 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3810 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3811 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3812 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3813 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3814 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3815 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3816 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3820 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3821 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3822 | return RValue<UInt4>(Nucleus::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3826 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3827 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3828 | return RValue<UInt4>(Nucleus::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3832 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3833 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3834 | return RValue<UInt4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3838 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3839 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3840 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3844 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3845 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3846 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3850 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3851 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3852 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3853 | 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] | 3854 | ::basicBlock->appendInst(cmp); |
| 3855 | |
| 3856 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3857 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3858 | ::basicBlock->appendInst(select); |
| 3859 | |
| 3860 | return RValue<UInt4>(V(result)); |
| 3861 | } |
| 3862 | |
| 3863 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3864 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3865 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3866 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3867 | 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] | 3868 | ::basicBlock->appendInst(cmp); |
| 3869 | |
| 3870 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3871 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3872 | ::basicBlock->appendInst(select); |
| 3873 | |
| 3874 | return RValue<UInt4>(V(result)); |
| 3875 | } |
| 3876 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3877 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3878 | { |
| 3879 | return T(Ice::IceType_v4i32); |
| 3880 | } |
| 3881 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3882 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3883 | { |
| 3884 | return T(Ice::IceType_i16); |
| 3885 | } |
| 3886 | |
| 3887 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3888 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3889 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3890 | return 1.0f / x; |
| 3891 | } |
| 3892 | |
| 3893 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3894 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3895 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3896 | return Rcp_pp(Sqrt(x)); |
| 3897 | } |
| 3898 | |
| 3899 | RValue<Float> Sqrt(RValue<Float> x) |
| 3900 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3901 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3902 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3903 | 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] | 3904 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3905 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3906 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3907 | ::basicBlock->appendInst(sqrt); |
| 3908 | |
| 3909 | return RValue<Float>(V(result)); |
| 3910 | } |
| 3911 | |
| 3912 | RValue<Float> Round(RValue<Float> x) |
| 3913 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3914 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3915 | return Float4(Round(Float4(x))).x; |
| 3916 | } |
| 3917 | |
| 3918 | RValue<Float> Trunc(RValue<Float> x) |
| 3919 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3920 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3921 | return Float4(Trunc(Float4(x))).x; |
| 3922 | } |
| 3923 | |
| 3924 | RValue<Float> Frac(RValue<Float> x) |
| 3925 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3926 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3927 | return Float4(Frac(Float4(x))).x; |
| 3928 | } |
| 3929 | |
| 3930 | RValue<Float> Floor(RValue<Float> x) |
| 3931 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3932 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3933 | return Float4(Floor(Float4(x))).x; |
| 3934 | } |
| 3935 | |
| 3936 | RValue<Float> Ceil(RValue<Float> x) |
| 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 | return Float4(Ceil(Float4(x))).x; |
| 3940 | } |
| 3941 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3942 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3943 | { |
| 3944 | return T(Ice::IceType_f32); |
| 3945 | } |
| 3946 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3947 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3948 | { |
| 3949 | return T(Type_v2f32); |
| 3950 | } |
| 3951 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3952 | Float4::Float4(RValue<Float> rhs) |
| 3953 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3954 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3955 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3956 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3957 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3958 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3959 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3960 | |
| 3961 | storeValue(replicate); |
| 3962 | } |
| 3963 | |
| 3964 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3965 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3966 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3967 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3968 | 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] | 3969 | ::basicBlock->appendInst(cmp); |
| 3970 | |
| 3971 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3972 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3973 | ::basicBlock->appendInst(select); |
| 3974 | |
| 3975 | return RValue<Float4>(V(result)); |
| 3976 | } |
| 3977 | |
| 3978 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3979 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3980 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3981 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3982 | 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] | 3983 | ::basicBlock->appendInst(cmp); |
| 3984 | |
| 3985 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3986 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3987 | ::basicBlock->appendInst(select); |
| 3988 | |
| 3989 | return RValue<Float4>(V(result)); |
| 3990 | } |
| 3991 | |
| 3992 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3993 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3994 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3995 | return Float4(1.0f) / x; |
| 3996 | } |
| 3997 | |
| 3998 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3999 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4000 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4001 | return Rcp_pp(Sqrt(x)); |
| 4002 | } |
| 4003 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 4004 | bool HasRcpApprox() |
| 4005 | { |
| 4006 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4007 | return false; |
| 4008 | } |
| 4009 | |
| 4010 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 4011 | { |
| 4012 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4013 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 4014 | return { 0.0f }; |
| 4015 | } |
| 4016 | |
| 4017 | RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2) |
| 4018 | { |
| 4019 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4020 | UNREACHABLE("RValue<Float> RcpApprox()"); |
| 4021 | return { 0.0f }; |
| 4022 | } |
| 4023 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 4024 | bool HasRcpSqrtApprox() |
| 4025 | { |
| 4026 | return false; |
| 4027 | } |
| 4028 | |
| 4029 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 4030 | { |
| 4031 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4032 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 4033 | return { 0.0f }; |
| 4034 | } |
| 4035 | |
| 4036 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 4037 | { |
| 4038 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4039 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 4040 | return { 0.0f }; |
| 4041 | } |
| 4042 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4043 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4044 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4045 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4046 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4047 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4048 | Float4 result; |
| 4049 | result.x = Sqrt(Float(Float4(x).x)); |
| 4050 | result.y = Sqrt(Float(Float4(x).y)); |
| 4051 | result.z = Sqrt(Float(Float4(x).z)); |
| 4052 | result.w = Sqrt(Float(Float4(x).w)); |
| 4053 | |
| 4054 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4055 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4056 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4057 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4058 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4059 | 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] | 4060 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4061 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4062 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4063 | ::basicBlock->appendInst(sqrt); |
| 4064 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4065 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4066 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4067 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4068 | |
| 4069 | RValue<Int> SignMask(RValue<Float4> x) |
| 4070 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4071 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4072 | if(emulateIntrinsics || CPUID::ARM) |
| 4073 | { |
| 4074 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4075 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4076 | } |
| 4077 | else |
| 4078 | { |
| 4079 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4080 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4081 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4082 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4083 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4084 | ::basicBlock->appendInst(movmsk); |
| 4085 | |
| 4086 | return RValue<Int>(V(result)); |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | RValue<Int4> CmpEQ(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::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | RValue<Int4> CmpLT(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::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | RValue<Int4> CmpLE(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::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | RValue<Int4> CmpNEQ(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::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | RValue<Int4> CmpNLT(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::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 4121 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4122 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4123 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 4127 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4128 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4129 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 4133 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4134 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4135 | return RValue<Int4>(Nucleus::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 4139 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4140 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4141 | return RValue<Int4>(Nucleus::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4145 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4146 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4147 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4151 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4152 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4153 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4157 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4158 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4159 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | RValue<Float4> Round(RValue<Float4> x) |
| 4163 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4164 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4165 | if(emulateIntrinsics || CPUID::ARM) |
| 4166 | { |
| 4167 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4168 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4169 | } |
| 4170 | else if(CPUID::SSE4_1) |
| 4171 | { |
| 4172 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4173 | 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] | 4174 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4175 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4176 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4177 | round->addArg(::context->getConstantInt32(0)); |
| 4178 | ::basicBlock->appendInst(round); |
| 4179 | |
| 4180 | return RValue<Float4>(V(result)); |
| 4181 | } |
| 4182 | else |
| 4183 | { |
| 4184 | return Float4(RoundInt(x)); |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | RValue<Float4> Trunc(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(3)); |
| 4199 | ::basicBlock->appendInst(round); |
| 4200 | |
| 4201 | return RValue<Float4>(V(result)); |
| 4202 | } |
| 4203 | else |
| 4204 | { |
| 4205 | return Float4(Int4(x)); |
| 4206 | } |
| 4207 | } |
| 4208 | |
| 4209 | RValue<Float4> Frac(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 | Float4 frc; |
| 4213 | |
| 4214 | if(CPUID::SSE4_1) |
| 4215 | { |
| 4216 | frc = x - Floor(x); |
| 4217 | } |
| 4218 | else |
| 4219 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4220 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4221 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4222 | 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] | 4223 | } |
| 4224 | |
| 4225 | // x - floor(x) can be 1.0 for very small negative x. |
| 4226 | // Clamp against the value just below 1.0. |
| 4227 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4228 | } |
| 4229 | |
| 4230 | RValue<Float4> Floor(RValue<Float4> x) |
| 4231 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4232 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4233 | if(CPUID::SSE4_1) |
| 4234 | { |
| 4235 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4236 | 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] | 4237 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4238 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4239 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4240 | round->addArg(::context->getConstantInt32(1)); |
| 4241 | ::basicBlock->appendInst(round); |
| 4242 | |
| 4243 | return RValue<Float4>(V(result)); |
| 4244 | } |
| 4245 | else |
| 4246 | { |
| 4247 | return x - Frac(x); |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4252 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4253 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4254 | if(CPUID::SSE4_1) |
| 4255 | { |
| 4256 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4257 | 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] | 4258 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4259 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4260 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4261 | round->addArg(::context->getConstantInt32(2)); |
| 4262 | ::basicBlock->appendInst(round); |
| 4263 | |
| 4264 | return RValue<Float4>(V(result)); |
| 4265 | } |
| 4266 | else |
| 4267 | { |
| 4268 | return -Floor(-x); |
| 4269 | } |
| 4270 | } |
| 4271 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4272 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4273 | { |
| 4274 | return T(Ice::IceType_v4f32); |
| 4275 | } |
| 4276 | |
| 4277 | RValue<Long> Ticks() |
| 4278 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4279 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4280 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4281 | return Long(Int(0)); |
| 4282 | } |
| 4283 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4284 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4285 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4286 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4287 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4288 | } |
| 4289 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4290 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4291 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4292 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4293 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4294 | } |
| 4295 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4296 | 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] | 4297 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4299 | 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] | 4300 | } |
| 4301 | |
| 4302 | void Breakpoint() |
| 4303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4305 | 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] | 4306 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4307 | auto trap = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4308 | ::basicBlock->appendInst(trap); |
| 4309 | } |
| 4310 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4311 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4312 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4313 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4314 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicFence, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4315 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4316 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4317 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4318 | inst->addArg(order); |
| 4319 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4320 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4321 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4322 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4323 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4324 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4325 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4326 | return nullptr; |
| 4327 | } |
| 4328 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4329 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4330 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4331 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4332 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4333 | |
| 4334 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4335 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4337 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4338 | } |
| 4339 | |
| 4340 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4343 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4344 | } |
| 4345 | |
| 4346 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4347 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4348 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4349 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4350 | } |
| 4351 | |
| 4352 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4353 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4355 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4356 | } |
| 4357 | |
| 4358 | RValue<Float> Exp2(RValue<Float> x) |
| 4359 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4361 | return emulated::Exp2(x); |
| 4362 | } |
| 4363 | |
| 4364 | RValue<Float> Log2(RValue<Float> x) |
| 4365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4367 | return emulated::Log2(x); |
| 4368 | } |
| 4369 | |
| 4370 | RValue<Float4> Sin(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::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | RValue<Float4> Cos(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::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | RValue<Float4> Tan(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::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4386 | } |
| 4387 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4388 | RValue<Float4> Asin(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4389 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4390 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4391 | if(p == Precision::Full) |
| 4392 | { |
| 4393 | return emulated::Asin(x); |
| 4394 | } |
| 4395 | return optimal::Asin_8_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4396 | } |
| 4397 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4398 | RValue<Float4> Acos(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4399 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4400 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4401 | // Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial approximation |
| 4402 | // version of acos, unlike for Asin, which requires higher precision algorithms. |
| 4403 | return optimal::Acos_4_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | RValue<Float4> Atan(RValue<Float4> x) |
| 4407 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4408 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4409 | return optimal::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4413 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4415 | return optimal::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | RValue<Float4> Cosh(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::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | RValue<Float4> Tanh(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::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | RValue<Float4> Asinh(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::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | RValue<Float4> Acosh(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::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4445 | return optimal::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 4449 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4450 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4451 | return optimal::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4455 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4457 | return optimal::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | RValue<Float4> Exp(RValue<Float4> x) |
| 4461 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4463 | return optimal::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | RValue<Float4> Log(RValue<Float4> x) |
| 4467 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4468 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4469 | return optimal::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4473 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4474 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4475 | return optimal::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | RValue<Float4> Log2(RValue<Float4> x) |
| 4479 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4480 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4481 | return optimal::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4485 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4486 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4487 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4488 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4489 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4490 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4491 | } |
| 4492 | else |
| 4493 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4494 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4495 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Ctlz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4496 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4497 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4498 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4499 | ::basicBlock->appendInst(ctlz); |
| 4500 | |
| 4501 | return RValue<UInt>(V(result)); |
| 4502 | } |
| 4503 | } |
| 4504 | |
| 4505 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4506 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4507 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4508 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4509 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4510 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4511 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4512 | } |
| 4513 | else |
| 4514 | { |
| 4515 | // TODO: implement vectorized version in Subzero |
| 4516 | UInt4 result; |
| 4517 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4518 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4519 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4520 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4521 | return result; |
| 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4526 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4527 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4528 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4529 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4530 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4531 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4532 | } |
| 4533 | else |
| 4534 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4535 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4536 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Cttz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4537 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4538 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4539 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4540 | ::basicBlock->appendInst(ctlz); |
| 4541 | |
| 4542 | return RValue<UInt>(V(result)); |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4547 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4548 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4549 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4550 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4551 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4552 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4553 | } |
| 4554 | else |
| 4555 | { |
| 4556 | // TODO: implement vectorized version in Subzero |
| 4557 | UInt4 result; |
| 4558 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4559 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4560 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4561 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4562 | return result; |
| 4563 | } |
| 4564 | } |
| 4565 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4566 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4567 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4568 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4569 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4570 | } |
| 4571 | |
| 4572 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4573 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4574 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4575 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4576 | } |
| 4577 | |
| 4578 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4579 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4580 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4581 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4582 | } |
| 4583 | |
| 4584 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4585 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4586 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4587 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4588 | } |
| 4589 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4590 | void EmitDebugLocation() |
| 4591 | { |
| 4592 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4593 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4594 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4595 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4596 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4597 | void FlushDebug() {} |
| 4598 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4599 | namespace { |
| 4600 | namespace coro { |
| 4601 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4602 | // Instance data per generated coroutine |
| 4603 | // This is the "handle" type used for Coroutine functions |
| 4604 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4605 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4606 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4607 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4608 | bool done = false; // the coroutine should stop at the next yield() |
| 4609 | bool terminated = false; // the coroutine has finished. |
| 4610 | bool inRoutine = false; // is the coroutine currently executing? |
| 4611 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4612 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4613 | void *promisePtr = nullptr; |
| 4614 | }; |
| 4615 | |
| 4616 | CoroutineData *createCoroutineData() |
| 4617 | { |
| 4618 | return new CoroutineData{}; |
| 4619 | } |
| 4620 | |
| 4621 | void destroyCoroutineData(CoroutineData *coroData) |
| 4622 | { |
| 4623 | delete coroData; |
| 4624 | } |
| 4625 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4626 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4627 | // caller's call to await(). |
| 4628 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4629 | // is called. |
| 4630 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4631 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4632 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4633 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4634 | ASSERT(coroData->inRoutine); |
| 4635 | coroData->inRoutine = false; |
| 4636 | coroData->mainFiber->notify(); |
| 4637 | while(!coroData->inRoutine) |
| 4638 | { |
| 4639 | coroData->routineFiber->wait(); |
| 4640 | } |
| 4641 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4642 | } |
| 4643 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4644 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4645 | // or the coroutine terminates. |
| 4646 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4647 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4648 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4649 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4650 | ASSERT(!coroData->inRoutine); |
| 4651 | coroData->inRoutine = true; |
| 4652 | coroData->routineFiber->notify(); |
| 4653 | while(coroData->inRoutine) |
| 4654 | { |
| 4655 | coroData->mainFiber->wait(); |
| 4656 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4657 | } |
| 4658 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4659 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4660 | // until the coroutine ends, and deletes the coroutine data. |
| 4661 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4662 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4663 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4664 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4665 | ASSERT(!coroData->inRoutine); |
| 4666 | if(!coroData->terminated) |
| 4667 | { |
| 4668 | coroData->done = true; |
| 4669 | coroData->inRoutine = true; |
| 4670 | coroData->routineFiber->notify(); |
| 4671 | while(!coroData->terminated) |
| 4672 | { |
| 4673 | coroData->mainFiber->wait(); |
| 4674 | } |
| 4675 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4676 | if(coroData->useInternalScheduler) |
| 4677 | { |
| 4678 | ::getOrCreateScheduler().unbind(); |
| 4679 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4680 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | namespace detail { |
| 4684 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4685 | } // namespace detail |
| 4686 | |
| 4687 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4688 | { |
| 4689 | ASSERT(!detail::coroHandle); |
| 4690 | detail::coroHandle = handle; |
| 4691 | } |
| 4692 | |
| 4693 | Nucleus::CoroutineHandle getHandleParam() |
| 4694 | { |
| 4695 | ASSERT(detail::coroHandle); |
| 4696 | auto handle = detail::coroHandle; |
| 4697 | detail::coroHandle = {}; |
| 4698 | return handle; |
| 4699 | } |
| 4700 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4701 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4702 | { |
| 4703 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4704 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4708 | { |
| 4709 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4710 | coroData->promisePtr = promisePtr; |
| 4711 | } |
| 4712 | |
| 4713 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4714 | { |
| 4715 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4716 | return coroData->promisePtr; |
| 4717 | } |
| 4718 | |
| 4719 | } // namespace coro |
| 4720 | } // namespace |
| 4721 | |
| 4722 | // Used to generate coroutines. |
| 4723 | // Lifetime: from yield to acquireCoroutine |
| 4724 | class CoroutineGenerator |
| 4725 | { |
| 4726 | public: |
| 4727 | CoroutineGenerator() |
| 4728 | { |
| 4729 | } |
| 4730 | |
| 4731 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4732 | void generateCoroutineBegin() |
| 4733 | { |
| 4734 | // Begin building the main coroutine_begin() function. |
| 4735 | // We insert these instructions at the top of the entry node, |
| 4736 | // before existing reactor-generated instructions. |
| 4737 | |
| 4738 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4739 | // { |
| 4740 | // this->handle = coro::getHandleParam(); |
| 4741 | // |
| 4742 | // YieldType promise; |
| 4743 | // coro::setPromisePtr(handle, &promise); // For await |
| 4744 | // |
| 4745 | // ... <REACTOR CODE> ... |
| 4746 | // |
| 4747 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4748 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4749 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4750 | |
| 4751 | // YieldType promise; |
| 4752 | // coro::setPromisePtr(handle, &promise); // For await |
| 4753 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4754 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4758 | void generateYield(Value *val) |
| 4759 | { |
| 4760 | // ... <REACTOR CODE> ... |
| 4761 | // |
| 4762 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4763 | // if (!coro::suspend(handle)) { |
| 4764 | // return false; // coroutine has been stopped by the caller. |
| 4765 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4766 | // |
| 4767 | // ... <REACTOR CODE> ... |
| 4768 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4769 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4770 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4771 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4772 | // if (!coro::suspend(handle)) { |
| 4773 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4774 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4775 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4776 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4777 | |
| 4778 | // return false; // coroutine has been stopped by the caller. |
| 4779 | ::basicBlock = doneBlock; |
| 4780 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4781 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4782 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4783 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4787 | |
| 4788 | // Generates the await function for the current coroutine. |
| 4789 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4790 | static FunctionUniquePtr generateAwaitFunction() |
| 4791 | { |
| 4792 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4793 | // { |
| 4794 | // if (coro::isDone()) |
| 4795 | // { |
| 4796 | // return false; |
| 4797 | // } |
| 4798 | // else // resume |
| 4799 | // { |
| 4800 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4801 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4802 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4803 | // return true; |
| 4804 | // } |
| 4805 | // } |
| 4806 | |
| 4807 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4808 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4809 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4810 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4811 | |
| 4812 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4813 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4814 | |
| 4815 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4816 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4817 | |
| 4818 | auto doneBlock = awaitFunc->makeNode(); |
| 4819 | { |
| 4820 | // return false; |
| 4821 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4822 | doneBlock->appendInst(ret); |
| 4823 | } |
| 4824 | |
| 4825 | auto resumeBlock = awaitFunc->makeNode(); |
| 4826 | { |
| 4827 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4828 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4829 | |
| 4830 | // *out = *promise; |
| 4831 | // Load promise value |
| 4832 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4833 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4834 | resumeBlock->appendInst(load); |
| 4835 | // Then store it in output param |
| 4836 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4837 | resumeBlock->appendInst(store); |
| 4838 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4839 | // coro::resume(handle); |
| 4840 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4841 | |
| 4842 | // return true; |
| 4843 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4844 | resumeBlock->appendInst(ret); |
| 4845 | } |
| 4846 | |
| 4847 | // if (coro::isDone()) |
| 4848 | // { |
| 4849 | // <doneBlock> |
| 4850 | // } |
| 4851 | // else // resume |
| 4852 | // { |
| 4853 | // <resumeBlock> |
| 4854 | // } |
| 4855 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4856 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4857 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4858 | bb->appendInst(br); |
| 4859 | |
| 4860 | return FunctionUniquePtr{ awaitFunc }; |
| 4861 | } |
| 4862 | |
| 4863 | // Generates the destroy function for the current coroutine. |
| 4864 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4865 | static FunctionUniquePtr generateDestroyFunction() |
| 4866 | { |
| 4867 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4868 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4869 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4870 | // return; |
| 4871 | // } |
| 4872 | |
| 4873 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4874 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4875 | |
| 4876 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4877 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4878 | |
| 4879 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4880 | |
| 4881 | auto *bb = destroyFunc->getEntryNode(); |
| 4882 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4883 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4884 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4885 | |
| 4886 | // return; |
| 4887 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4888 | bb->appendInst(ret); |
| 4889 | |
| 4890 | return FunctionUniquePtr{ destroyFunc }; |
| 4891 | } |
| 4892 | |
| 4893 | private: |
| 4894 | Ice::Variable *handle{}; |
| 4895 | Ice::Variable *promise{}; |
| 4896 | }; |
| 4897 | |
| 4898 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4899 | { |
| 4900 | // This doubles up as our coroutine handle |
| 4901 | auto coroData = coro::createCoroutineData(); |
| 4902 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4903 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4904 | if(coroData->useInternalScheduler) |
| 4905 | { |
| 4906 | ::getOrCreateScheduler().bind(); |
| 4907 | } |
| 4908 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4909 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4910 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4911 | // any fiber switch occurs. |
| 4912 | coro::setHandleParam(coroData); |
| 4913 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4914 | ASSERT(!coroData->routineFiber); |
| 4915 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4916 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4917 | beginFunc(); |
| 4918 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4919 | ASSERT(coroData->inRoutine); |
| 4920 | coroData->done = true; // coroutine is done. |
| 4921 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4922 | coroData->inRoutine = false; |
| 4923 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4924 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4925 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4926 | ASSERT(!coroData->mainFiber); |
| 4927 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4928 | |
| 4929 | // block until the first yield or coroutine end |
| 4930 | ASSERT(!coroData->inRoutine); |
| 4931 | coroData->inRoutine = true; |
| 4932 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4933 | while(coroData->inRoutine) |
| 4934 | { |
| 4935 | coroData->mainFiber->wait(); |
| 4936 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4937 | |
| 4938 | return coroData; |
| 4939 | } |
| 4940 | |
| 4941 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4942 | { |
| 4943 | // Start by creating a regular function |
| 4944 | createFunction(yieldType, params); |
| 4945 | |
| 4946 | // Save in case yield() is called |
| 4947 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4948 | ::coroYieldType = yieldType; |
| 4949 | } |
| 4950 | |
| 4951 | void Nucleus::yield(Value *val) |
| 4952 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4953 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4954 | Variable::materializeAll(); |
| 4955 | |
| 4956 | // On first yield, we start generating coroutine functions |
| 4957 | if(!::coroGen) |
| 4958 | { |
| 4959 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4960 | ::coroGen->generateCoroutineBegin(); |
| 4961 | } |
| 4962 | |
| 4963 | ASSERT(::coroGen); |
| 4964 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4965 | } |
| 4966 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4967 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4968 | { |
| 4969 | return false; |
| 4970 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4971 | |
| 4972 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4973 | { |
| 4974 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4975 | |
| 4976 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 4977 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4978 | if(::coroGen) |
| 4979 | { |
| 4980 | // Finish generating coroutine functions |
| 4981 | { |
| 4982 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4983 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4984 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4985 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4986 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4987 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4988 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4989 | // At this point, we no longer need the CoroutineGenerator. |
| 4990 | ::coroGen.reset(); |
| 4991 | ::coroYieldType = nullptr; |
| 4992 | |
| 4993 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4994 | { name, "await", "destroy" }, |
| 4995 | cfgEdit); |
| 4996 | |
| 4997 | return routine; |
| 4998 | } |
| 4999 | else |
| 5000 | { |
| 5001 | { |
| 5002 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 5003 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | ::coroYieldType = nullptr; |
| 5007 | |
| 5008 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 5009 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 5010 | |
| 5011 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 5012 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 5013 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 5014 | return routine; |
| 5015 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5016 | } |
| 5017 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5018 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5019 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5020 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 5021 | |
| 5022 | if(isCoroutine) |
| 5023 | { |
| 5024 | return rr::invokeCoroutineBegin(func); |
| 5025 | } |
| 5026 | else |
| 5027 | { |
| 5028 | // For regular routines, just invoke the begin func directly |
| 5029 | return func(); |
| 5030 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5031 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5032 | |
| 5033 | } // namespace rr |