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; |
| 67 | auto function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 68 | |
| 69 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 70 | |
| 71 | for(auto type : paramTypes) |
| 72 | { |
| 73 | Ice::Variable *arg = function->makeVariable(type); |
| 74 | function->addArg(arg); |
| 75 | } |
| 76 | |
| 77 | Ice::CfgNode *node = function->makeNode(); |
| 78 | function->setEntryNode(node); |
| 79 | |
| 80 | return function; |
| 81 | } |
| 82 | |
| 83 | Ice::Type getPointerType(Ice::Type elementType) |
| 84 | { |
| 85 | if(sizeof(void *) == 8) |
| 86 | { |
| 87 | return Ice::IceType_i64; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | return Ice::IceType_i32; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 96 | { |
| 97 | int typeSize = Ice::typeWidthInBytes(type); |
| 98 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 99 | |
| 100 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 101 | auto address = function->makeVariable(getPointerType(type)); |
| 102 | auto alloca = Ice::InstAlloca::create(function, address, bytes, typeSize); |
| 103 | function->getEntryNode()->getInsts().push_front(alloca); |
| 104 | |
| 105 | return address; |
| 106 | } |
| 107 | |
| 108 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 109 | { |
| 110 | if(sizeof(void *) == 8) |
| 111 | { |
| 112 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 117 | } |
| 118 | } |
| 119 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 120 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 121 | 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] | 122 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 123 | // Wrapper for calls on C functions with Ice types |
| 124 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 125 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 126 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 127 | |
| 128 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 129 | // then truncate result to bool. |
| 130 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 131 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 132 | if(returningBool) |
| 133 | { |
| 134 | ret = function->makeVariable(Ice::IceType_i32); |
| 135 | } |
| 136 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 137 | { |
| 138 | ret = function->makeVariable(retTy); |
| 139 | } |
| 140 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 141 | 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] | 142 | for(auto arg : iceArgs) |
| 143 | { |
| 144 | call->addArg(arg); |
| 145 | } |
| 146 | |
| 147 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 148 | |
| 149 | if(returningBool) |
| 150 | { |
| 151 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 152 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 153 | } |
| 154 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 158 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 159 | { |
| 160 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 161 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 162 | } |
| 163 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 164 | // Wrapper for calls on C functions with Ice types |
| 165 | template<typename Return, typename... CArgs, typename... RArgs> |
| 166 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Return(fptr)(CArgs...), RArgs &&... args) |
| 167 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 168 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 169 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 170 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 171 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 172 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 175 | // Returns a non-const variable copy of const v |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 176 | Ice::Variable *createUnconstCast(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Constant *v) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 177 | { |
| 178 | Ice::Variable *result = function->makeVariable(v->getType()); |
| 179 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Bitcast, result, v); |
| 180 | basicBlock->appendInst(cast); |
| 181 | return result; |
| 182 | } |
| 183 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 184 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 185 | { |
| 186 | Ice::Variable *to = function->makeVariable(toType); |
| 187 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 188 | basicBlock->appendInst(cast); |
| 189 | return to; |
| 190 | } |
| 191 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 192 | 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] | 193 | { |
| 194 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 195 | // absolute address. We circumvent this by casting to a non-const variable, and loading |
| 196 | // from that. |
| 197 | if(auto *cptr = llvm::dyn_cast<Ice::Constant>(ptr)) |
| 198 | { |
| 199 | ptr = sz::createUnconstCast(function, basicBlock, cptr); |
| 200 | } |
| 201 | |
| 202 | Ice::Variable *result = function->makeVariable(type); |
| 203 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 204 | basicBlock->appendInst(load); |
| 205 | |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 210 | } // namespace |
| 211 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 212 | namespace rr { |
| 213 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 214 | class CoroutineGenerator; |
| 215 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 216 | |
| 217 | namespace { |
| 218 | |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame] | 219 | // Used to automatically invoke llvm_shutdown() when driver is unloaded |
| 220 | llvm::llvm_shutdown_obj llvmShutdownObj; |
| 221 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 222 | // Default configuration settings. Must be accessed under mutex lock. |
| 223 | std::mutex defaultConfigLock; |
| 224 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 225 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 226 | // This uses a static in a function to avoid the cost of a global static |
| 227 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 228 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 229 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 230 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 233 | Ice::GlobalContext *context = nullptr; |
| 234 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 235 | Ice::CfgNode *entryBlock = nullptr; |
| 236 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 237 | Ice::CfgNode *basicBlock = nullptr; |
| 238 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 239 | rr::ELFMemoryStreamer *routine = nullptr; |
| 240 | |
| 241 | std::mutex codegenMutex; |
| 242 | |
| 243 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 244 | Ice::Fdstream *out = nullptr; |
| 245 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 246 | // Coroutine globals |
| 247 | rr::Type *coroYieldType = nullptr; |
| 248 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 249 | marl::Scheduler &getOrCreateScheduler() |
| 250 | { |
| 251 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 252 | marl::Scheduler::Config cfg; |
| 253 | cfg.setWorkerThreadCount(8); |
| 254 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 255 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 256 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 257 | return *scheduler; |
| 258 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 259 | } // Anonymous namespace |
| 260 | |
| 261 | namespace { |
| 262 | |
| 263 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 264 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 265 | #endif |
| 266 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 267 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 268 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 269 | #endif |
| 270 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 271 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 272 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 273 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 274 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 275 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 276 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 277 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 278 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 279 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 280 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 281 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 282 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 285 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 286 | { |
| 287 | switch(memoryOrder) |
| 288 | { |
| 289 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 290 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 291 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 292 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 293 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 294 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
| 295 | } |
| 296 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 297 | } |
| 298 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 299 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 300 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 301 | public: |
| 302 | const static bool ARM; |
| 303 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 304 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 305 | private: |
| 306 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 307 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 308 | #if defined(__i386__) || defined(__x86_64__) |
| 309 | # if defined(_WIN32) |
| 310 | __cpuid(registers, info); |
| 311 | # else |
| 312 | __asm volatile("cpuid" |
| 313 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 314 | : "a"(info)); |
| 315 | # endif |
| 316 | #else |
| 317 | registers[0] = 0; |
| 318 | registers[1] = 0; |
| 319 | registers[2] = 0; |
| 320 | registers[3] = 0; |
| 321 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 324 | static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 325 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 326 | #if defined(__arm__) || defined(__aarch64__) |
| 327 | return true; |
| 328 | #elif defined(__i386__) || defined(__x86_64__) |
| 329 | return false; |
| 330 | #elif defined(__mips__) |
| 331 | return false; |
| 332 | #else |
| 333 | # error "Unknown architecture" |
| 334 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 335 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 336 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 337 | static bool detectSSE4_1() |
| 338 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 339 | #if defined(__i386__) || defined(__x86_64__) |
| 340 | int registers[4]; |
| 341 | cpuid(registers, 1); |
| 342 | return (registers[2] & 0x00080000) != 0; |
| 343 | #else |
| 344 | return false; |
| 345 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 346 | } |
| 347 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 348 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 349 | const bool CPUID::ARM = CPUID::detectARM(); |
| 350 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
| 351 | const bool emulateIntrinsics = false; |
| 352 | const bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 353 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 354 | constexpr bool subzeroDumpEnabled = false; |
| 355 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 356 | |
| 357 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 358 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 359 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 360 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 361 | |
| 362 | } // anonymous namespace |
| 363 | |
| 364 | namespace rr { |
| 365 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 366 | std::string BackendName() |
| 367 | { |
| 368 | return "Subzero"; |
| 369 | } |
| 370 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 371 | const Capabilities Caps = { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 372 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | enum EmulatedType |
| 376 | { |
| 377 | EmulatedShift = 16, |
| 378 | EmulatedV2 = 2 << EmulatedShift, |
| 379 | EmulatedV4 = 4 << EmulatedShift, |
| 380 | EmulatedV8 = 8 << EmulatedShift, |
| 381 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 382 | |
| 383 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 384 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 385 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 386 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 387 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 388 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 389 | }; |
| 390 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 391 | class Value : public Ice::Operand |
| 392 | {}; |
| 393 | class SwitchCases : public Ice::InstSwitch |
| 394 | {}; |
| 395 | class BasicBlock : public Ice::CfgNode |
| 396 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 397 | |
| 398 | Ice::Type T(Type *t) |
| 399 | { |
| 400 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 401 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 404 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 405 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 406 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | Type *T(EmulatedType t) |
| 410 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 411 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 414 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 415 | { |
| 416 | std::vector<Ice::Type> result; |
| 417 | result.reserve(types.size()); |
| 418 | for(auto &t : types) |
| 419 | { |
| 420 | result.push_back(T(t)); |
| 421 | } |
| 422 | return result; |
| 423 | } |
| 424 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 425 | Value *V(Ice::Operand *v) |
| 426 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 427 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 430 | Ice::Operand *V(Value *v) |
| 431 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 432 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 433 | } |
| 434 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 435 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 436 | { |
| 437 | std::vector<Ice::Operand *> result; |
| 438 | result.reserve(values.size()); |
| 439 | for(auto &v : values) |
| 440 | { |
| 441 | result.push_back(V(v)); |
| 442 | } |
| 443 | return result; |
| 444 | } |
| 445 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 446 | BasicBlock *B(Ice::CfgNode *b) |
| 447 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 448 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static size_t typeSize(Type *type) |
| 452 | { |
| 453 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 454 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 455 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 456 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 457 | case Type_v2i32: return 8; |
| 458 | case Type_v4i16: return 8; |
| 459 | case Type_v2i16: return 4; |
| 460 | case Type_v8i8: return 8; |
| 461 | case Type_v4i8: return 4; |
| 462 | case Type_v2f32: return 8; |
| 463 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | return Ice::typeWidthInBytes(T(type)); |
| 468 | } |
| 469 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 470 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 471 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 472 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 473 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 474 | { |
| 475 | Nucleus::createRetVoid(); |
| 476 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 477 | |
| 478 | // Connect the entry block to the top of the initial basic block |
| 479 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 480 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 483 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 484 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 485 | |
| 486 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 487 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 488 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 492 | { |
| 493 | return §ionHeader(elfHeader)[index]; |
| 494 | } |
| 495 | |
| 496 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 497 | { |
| 498 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 499 | |
| 500 | uint32_t index = relocation.getSymbol(); |
| 501 | int table = relocationTable.sh_link; |
| 502 | void *symbolValue = nullptr; |
| 503 | |
| 504 | if(index != SHN_UNDEF) |
| 505 | { |
| 506 | if(table == SHN_UNDEF) return nullptr; |
| 507 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 508 | |
| 509 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 510 | if(index >= symtab_entries) |
| 511 | { |
| 512 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 513 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 516 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 517 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 518 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 519 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 521 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 522 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 523 | 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] | 524 | } |
| 525 | else |
| 526 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 528 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 531 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 532 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 533 | |
| 534 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 535 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 536 | switch(relocation.getType()) |
| 537 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 538 | case R_ARM_NONE: |
| 539 | // No relocation |
| 540 | break; |
| 541 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 542 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 543 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 544 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 545 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 546 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 547 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 548 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 549 | { |
| 550 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 551 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 552 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 553 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 554 | default: |
| 555 | ASSERT(false && "Unsupported relocation type"); |
| 556 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 557 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 558 | } |
| 559 | else |
| 560 | { |
| 561 | switch(relocation.getType()) |
| 562 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 563 | case R_386_NONE: |
| 564 | // No relocation |
| 565 | break; |
| 566 | case R_386_32: |
| 567 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 568 | break; |
| 569 | case R_386_PC32: |
| 570 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 571 | break; |
| 572 | default: |
| 573 | ASSERT(false && "Unsupported relocation type"); |
| 574 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 575 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 578 | return symbolValue; |
| 579 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 580 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 581 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 582 | { |
| 583 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 584 | |
| 585 | uint32_t index = relocation.getSymbol(); |
| 586 | int table = relocationTable.sh_link; |
| 587 | void *symbolValue = nullptr; |
| 588 | |
| 589 | if(index != SHN_UNDEF) |
| 590 | { |
| 591 | if(table == SHN_UNDEF) return nullptr; |
| 592 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 593 | |
| 594 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 595 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 596 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 597 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 598 | return nullptr; |
| 599 | } |
| 600 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 601 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 602 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 603 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 604 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 605 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 606 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 607 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 608 | 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] | 609 | } |
| 610 | else |
| 611 | { |
| 612 | return nullptr; |
| 613 | } |
| 614 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 615 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 616 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 617 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 618 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 619 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 620 | switch(relocation.getType()) |
| 621 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 622 | case R_X86_64_NONE: |
| 623 | // No relocation |
| 624 | break; |
| 625 | case R_X86_64_64: |
| 626 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 627 | break; |
| 628 | case R_X86_64_PC32: |
| 629 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 630 | break; |
| 631 | case R_X86_64_32S: |
| 632 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 633 | break; |
| 634 | default: |
| 635 | ASSERT(false && "Unsupported relocation type"); |
| 636 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | return symbolValue; |
| 640 | } |
| 641 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 642 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 643 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 644 | const void *entry; |
| 645 | size_t codeSize = 0; |
| 646 | }; |
| 647 | |
| 648 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 649 | { |
| 650 | ASSERT(functionNames.size() > 0); |
| 651 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 652 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 653 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 654 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 655 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 656 | if(!elfHeader->checkMagic()) |
| 657 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 658 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 662 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 663 | #if defined(__i386__) |
| 664 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 665 | #elif defined(__x86_64__) |
| 666 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 667 | #elif defined(__arm__) |
| 668 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 669 | #elif defined(__aarch64__) |
| 670 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 671 | #elif defined(__mips__) |
| 672 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 673 | #else |
| 674 | # error "Unsupported platform" |
| 675 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 676 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 677 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 678 | |
| 679 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 680 | { |
| 681 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 682 | { |
| 683 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 684 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 685 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 686 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 687 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 688 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 689 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 690 | { |
| 691 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 692 | { |
| 693 | return j; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 698 | return static_cast<size_t>(-1); |
| 699 | }; |
| 700 | |
| 701 | size_t index = findSectionNameEntryIndex(); |
| 702 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 703 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 704 | } |
| 705 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 706 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 707 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 708 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 709 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 710 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 711 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 712 | 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] | 713 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 714 | } |
| 715 | } |
| 716 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 717 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 718 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 719 | |
| 720 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 721 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 722 | 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] | 723 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 728 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | template<typename T> |
| 732 | struct ExecutableAllocator |
| 733 | { |
| 734 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 735 | template<class U> |
| 736 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 737 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 738 | |
| 739 | using value_type = T; |
| 740 | using size_type = std::size_t; |
| 741 | |
| 742 | T *allocate(size_type n) |
| 743 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 744 | return (T *)allocateMemoryPages( |
| 745 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void deallocate(T *p, size_type n) |
| 749 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 750 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 751 | } |
| 752 | }; |
| 753 | |
| 754 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 755 | { |
| 756 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 757 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 758 | |
| 759 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 760 | ELFMemoryStreamer() |
| 761 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 762 | { |
| 763 | position = 0; |
| 764 | buffer.reserve(0x1000); |
| 765 | } |
| 766 | |
| 767 | ~ELFMemoryStreamer() override |
| 768 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void write8(uint8_t Value) override |
| 772 | { |
| 773 | if(position == (uint64_t)buffer.size()) |
| 774 | { |
| 775 | buffer.push_back(Value); |
| 776 | position++; |
| 777 | } |
| 778 | else if(position < (uint64_t)buffer.size()) |
| 779 | { |
| 780 | buffer[position] = Value; |
| 781 | position++; |
| 782 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 783 | else |
| 784 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void writeBytes(llvm::StringRef Bytes) override |
| 788 | { |
| 789 | std::size_t oldSize = buffer.size(); |
| 790 | buffer.resize(oldSize + Bytes.size()); |
| 791 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 792 | position += Bytes.size(); |
| 793 | } |
| 794 | |
| 795 | uint64_t tell() const override { return position; } |
| 796 | |
| 797 | void seek(uint64_t Off) override { position = Off; } |
| 798 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 799 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 800 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 801 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 802 | |
| 803 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 804 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 805 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 806 | for(auto &entryPoint : entryPoints) |
| 807 | { |
| 808 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 809 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 810 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 811 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 812 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 815 | void finalize() |
| 816 | { |
| 817 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 818 | |
| 819 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 820 | } |
| 821 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 822 | void setEntry(int index, const void *func) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 823 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 824 | ASSERT(func); |
| 825 | funcs[index] = func; |
| 826 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 827 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 828 | const void *getEntry(int index) const override |
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 | ASSERT(funcs[index]); |
| 831 | return funcs[index]; |
| 832 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 833 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 834 | 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] | 835 | { |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 836 | // Check if we already have a suitable constant. |
| 837 | for(const auto &c : constantsPool) |
| 838 | { |
| 839 | void *ptr = c.data.get(); |
| 840 | size_t space = c.space; |
| 841 | |
| 842 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 843 | |
| 844 | if(space < size) |
| 845 | { |
| 846 | continue; |
| 847 | } |
| 848 | |
| 849 | if(memcmp(data, alignedPtr, size) == 0) |
| 850 | { |
| 851 | return alignedPtr; |
| 852 | } |
| 853 | } |
| 854 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 855 | // TODO(b/148086935): Replace with a buffer allocator. |
| 856 | size_t space = size + alignment; |
| 857 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 858 | void *ptr = buf.get(); |
| 859 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 860 | ASSERT(alignedPtr); |
| 861 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 862 | constantsPool.emplace_back(std::move(buf), space); |
| 863 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 864 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 865 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 866 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 867 | private: |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 868 | struct Constant |
| 869 | { |
| 870 | Constant(std::unique_ptr<uint8_t[]> data, size_t space) |
| 871 | : data(std::move(data)) |
| 872 | , space(space) |
| 873 | {} |
| 874 | |
| 875 | std::unique_ptr<uint8_t[]> data; |
| 876 | size_t space; |
| 877 | }; |
| 878 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 879 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 880 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 881 | std::size_t position; |
Nicolas Capens | 4e75f45 | 2021-01-28 01:52:56 -0500 | [diff] [blame] | 882 | std::vector<Constant> constantsPool; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 883 | }; |
| 884 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 885 | #ifdef ENABLE_RR_PRINT |
| 886 | void VPrintf(const std::vector<Value *> &vals) |
| 887 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 888 | 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] | 889 | } |
| 890 | #endif // ENABLE_RR_PRINT |
| 891 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 892 | Nucleus::Nucleus() |
| 893 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 894 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 895 | |
| 896 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 897 | Ice::ClFlags::getParsedClFlags(Flags); |
| 898 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 899 | #if defined(__arm__) |
| 900 | Flags.setTargetArch(Ice::Target_ARM32); |
| 901 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 902 | #elif defined(__mips__) |
| 903 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 904 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 905 | #else // x86 |
| 906 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 907 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 908 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 909 | Flags.setOutFileType(Ice::FT_Elf); |
| 910 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
| 911 | Flags.setApplicationBinaryInterface(Ice::ABI_Platform); |
| 912 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 913 | Flags.setDisableHybridAssembly(true); |
| 914 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 915 | // Emit functions into separate sections in the ELF so we can find them by name |
| 916 | Flags.setFunctionSections(true); |
| 917 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 918 | static llvm::raw_os_ostream cout(std::cout); |
| 919 | static llvm::raw_os_ostream cerr(std::cerr); |
| 920 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 921 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 922 | { |
| 923 | // Decorate text asm with liveness info |
| 924 | Flags.setDecorateAsm(true); |
| 925 | } |
| 926 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 927 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 928 | { |
| 929 | std::error_code errorCode; |
| 930 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 931 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 932 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 937 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 938 | ::routine = elfMemory; |
| 939 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 940 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 941 | #if !__has_feature(memory_sanitizer) |
| 942 | // thread_local variables in shared libraries are initialized at load-time, |
| 943 | // but this is not observed by MemorySanitizer if the loader itself was not |
| 944 | // instrumented, leading to false-positive unitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 945 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 946 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 947 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | Nucleus::~Nucleus() |
| 951 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 952 | delete Variable::unmaterializedVariables; |
| 953 | Variable::unmaterializedVariables = nullptr; |
| 954 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 955 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 956 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 957 | |
| 958 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 959 | ::allocator = nullptr; |
| 960 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 961 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 962 | ::function = nullptr; |
| 963 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 964 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 965 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 966 | |
| 967 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 968 | ::elfFile = nullptr; |
| 969 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 970 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 971 | ::out = nullptr; |
| 972 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 973 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 974 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 975 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 976 | |
| 977 | ::codegenMutex.unlock(); |
| 978 | } |
| 979 | |
| 980 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 981 | { |
| 982 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 983 | ::defaultConfig() = cfg; |
| 984 | } |
| 985 | |
| 986 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 987 | { |
| 988 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 989 | auto &config = ::defaultConfig(); |
| 990 | config = cfgEdit.apply(config); |
| 991 | } |
| 992 | |
| 993 | Config Nucleus::getDefaultConfig() |
| 994 | { |
| 995 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 996 | return ::defaultConfig(); |
| 997 | } |
| 998 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 999 | // This function lowers and produces executable binary code in memory for the input functions, |
| 1000 | // and returns a Routine with the entry points to these functions. |
| 1001 | template<size_t Count> |
| 1002 | 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] | 1003 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1004 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 1005 | // and GlobalContext::emitItems. |
| 1006 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1007 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1008 | { |
| 1009 | // 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] | 1010 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | ::context->emitFileHeader(); |
| 1014 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1015 | // Translate |
| 1016 | |
| 1017 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1018 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1019 | Ice::Cfg *currFunc = functions[i]; |
| 1020 | |
| 1021 | // Install function allocator in TLS for Cfg-specific container allocators |
| 1022 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 1023 | |
| 1024 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 1025 | |
| 1026 | rr::optimize(currFunc); |
| 1027 | |
| 1028 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1029 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1030 | |
| 1031 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1032 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1033 | |
| 1034 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1035 | |
| 1036 | if(subzeroEmitTextAsm) |
| 1037 | { |
| 1038 | currFunc->emit(); |
| 1039 | } |
| 1040 | |
| 1041 | currFunc->emitIAS(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1042 | } |
| 1043 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1044 | // Emit items |
| 1045 | |
| 1046 | ::context->lowerGlobals(""); |
| 1047 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1048 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1049 | |
| 1050 | for(size_t i = 0; i < Count; ++i) |
| 1051 | { |
| 1052 | Ice::Cfg *currFunc = functions[i]; |
| 1053 | |
| 1054 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1055 | auto globals = currFunc->getGlobalInits(); |
| 1056 | if(globals && !globals->empty()) |
| 1057 | { |
| 1058 | ::context->getGlobals()->merge(globals.get()); |
| 1059 | } |
| 1060 | |
| 1061 | auto assembler = currFunc->releaseAssembler(); |
| 1062 | assembler->alignFunction(); |
| 1063 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1064 | } |
| 1065 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1066 | ::context->lowerGlobals("last"); |
| 1067 | ::context->lowerConstants(); |
| 1068 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1069 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1070 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1071 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1072 | objectWriter->writeNonUserSections(); |
| 1073 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1074 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1075 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1076 | ASSERT(entryPoints.size() == Count); |
| 1077 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1078 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1079 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1083 | |
| 1084 | Routine *handoffRoutine = ::routine; |
| 1085 | ::routine = nullptr; |
| 1086 | |
| 1087 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1088 | } |
| 1089 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1090 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 1091 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1092 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1093 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1094 | } |
| 1095 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1096 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1097 | { |
| 1098 | Ice::Type type = T(t); |
| 1099 | int typeSize = Ice::typeWidthInBytes(type); |
| 1100 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1101 | |
| 1102 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1103 | auto address = ::function->makeVariable(T(getPointerType(t))); |
| 1104 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); |
| 1105 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1106 | |
| 1107 | return V(address); |
| 1108 | } |
| 1109 | |
| 1110 | BasicBlock *Nucleus::createBasicBlock() |
| 1111 | { |
| 1112 | return B(::function->makeNode()); |
| 1113 | } |
| 1114 | |
| 1115 | BasicBlock *Nucleus::getInsertBlock() |
| 1116 | { |
| 1117 | return B(::basicBlock); |
| 1118 | } |
| 1119 | |
| 1120 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1121 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1122 | // 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] | 1123 | |
| 1124 | Variable::materializeAll(); |
| 1125 | |
| 1126 | ::basicBlock = basicBlock; |
| 1127 | } |
| 1128 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1129 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1130 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1131 | ASSERT(::function == nullptr); |
| 1132 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1133 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1134 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1135 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1136 | |
| 1137 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1138 | |
| 1139 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1140 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1141 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1142 | // TODO: Get rid of this as a global, and create scoped allocs in every Nucleus function instead. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1143 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1144 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1145 | ::entryBlock = ::function->getEntryNode(); |
| 1146 | ::basicBlock = ::function->makeNode(); |
| 1147 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | Value *Nucleus::getArgument(unsigned int index) |
| 1151 | { |
| 1152 | return V(::function->getArgs()[index]); |
| 1153 | } |
| 1154 | |
| 1155 | void Nucleus::createRetVoid() |
| 1156 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1157 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1158 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1159 | // Code generated after this point is unreachable, so any variables |
| 1160 | // being read can safely return an undefined value. We have to avoid |
| 1161 | // materializing variables after the terminator ret instruction. |
| 1162 | Variable::killUnmaterialized(); |
| 1163 | |
| 1164 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1165 | ::basicBlock->appendInst(ret); |
| 1166 | } |
| 1167 | |
| 1168 | void Nucleus::createRet(Value *v) |
| 1169 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1170 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1171 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1172 | // Code generated after this point is unreachable, so any variables |
| 1173 | // being read can safely return an undefined value. We have to avoid |
| 1174 | // materializing variables after the terminator ret instruction. |
| 1175 | Variable::killUnmaterialized(); |
| 1176 | |
| 1177 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1178 | ::basicBlock->appendInst(ret); |
| 1179 | } |
| 1180 | |
| 1181 | void Nucleus::createBr(BasicBlock *dest) |
| 1182 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1183 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1184 | Variable::materializeAll(); |
| 1185 | |
| 1186 | auto br = Ice::InstBr::create(::function, dest); |
| 1187 | ::basicBlock->appendInst(br); |
| 1188 | } |
| 1189 | |
| 1190 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1191 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1192 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1193 | Variable::materializeAll(); |
| 1194 | |
| 1195 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1196 | ::basicBlock->appendInst(br); |
| 1197 | } |
| 1198 | |
| 1199 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1200 | { |
| 1201 | switch(op) |
| 1202 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1203 | case Ice::InstArithmetic::Add: |
| 1204 | case Ice::InstArithmetic::Fadd: |
| 1205 | case Ice::InstArithmetic::Mul: |
| 1206 | case Ice::InstArithmetic::Fmul: |
| 1207 | case Ice::InstArithmetic::And: |
| 1208 | case Ice::InstArithmetic::Or: |
| 1209 | case Ice::InstArithmetic::Xor: |
| 1210 | return true; |
| 1211 | default: |
| 1212 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1217 | { |
| 1218 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1219 | |
| 1220 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1221 | |
| 1222 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1223 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1224 | ::basicBlock->appendInst(arithmetic); |
| 1225 | |
| 1226 | return V(result); |
| 1227 | } |
| 1228 | |
| 1229 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1230 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1231 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1232 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
| 1233 | } |
| 1234 | |
| 1235 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1236 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1237 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1238 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
| 1239 | } |
| 1240 | |
| 1241 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1242 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1243 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1244 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
| 1245 | } |
| 1246 | |
| 1247 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1248 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1249 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1250 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
| 1251 | } |
| 1252 | |
| 1253 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1254 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1255 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1256 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
| 1257 | } |
| 1258 | |
| 1259 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1260 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1261 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1262 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
| 1263 | } |
| 1264 | |
| 1265 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1266 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1267 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1268 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
| 1269 | } |
| 1270 | |
| 1271 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1272 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1273 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1274 | return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs); |
| 1275 | } |
| 1276 | |
| 1277 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1278 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1279 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1280 | return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs); |
| 1281 | } |
| 1282 | |
| 1283 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1284 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1285 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1286 | return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs); |
| 1287 | } |
| 1288 | |
| 1289 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1290 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1291 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1292 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
| 1293 | } |
| 1294 | |
| 1295 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1296 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1297 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1298 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1299 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1300 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1301 | return nullptr; |
| 1302 | } |
| 1303 | |
| 1304 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1305 | { |
| 1306 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1310 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1311 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1312 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
| 1313 | } |
| 1314 | |
| 1315 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1316 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1317 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1318 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
| 1319 | } |
| 1320 | |
| 1321 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1322 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1323 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1324 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
| 1325 | } |
| 1326 | |
| 1327 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1328 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1329 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1330 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
| 1331 | } |
| 1332 | |
| 1333 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1334 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1335 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1336 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
| 1337 | } |
| 1338 | |
| 1339 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1340 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1341 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1342 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1343 | } |
| 1344 | |
| 1345 | Value *Nucleus::createNeg(Value *v) |
| 1346 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1347 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1348 | return createSub(createNullValue(T(v->getType())), v); |
| 1349 | } |
| 1350 | |
| 1351 | Value *Nucleus::createFNeg(Value *v) |
| 1352 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1353 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1354 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1355 | Value *negativeZero = Ice::isVectorType(v->getType()) ? createConstantVector(c, T(v->getType())) : V(::context->getConstantFloat(-0.0f)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1356 | |
| 1357 | return createFSub(negativeZero, v); |
| 1358 | } |
| 1359 | |
| 1360 | Value *Nucleus::createNot(Value *v) |
| 1361 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1362 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1363 | if(Ice::isScalarIntegerType(v->getType())) |
| 1364 | { |
| 1365 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1366 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1367 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1368 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1369 | int64_t c[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1370 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1371 | } |
| 1372 | } |
| 1373 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1374 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1375 | { |
| 1376 | #if defined(__i386__) || defined(__x86_64__) |
| 1377 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1378 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1379 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1380 | #else |
| 1381 | if(atomic) |
| 1382 | { |
| 1383 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1384 | } |
| 1385 | if(memoryOrder != std::memory_order_relaxed) |
| 1386 | { |
| 1387 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1388 | } |
| 1389 | #endif |
| 1390 | |
| 1391 | // Vulkan doesn't allow sequential memory order |
| 1392 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1393 | } |
| 1394 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1395 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1396 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1397 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1398 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1399 | |
| 1400 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1401 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1402 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1403 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1404 | { |
| 1405 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1406 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1407 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1408 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1409 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1410 | Int x = *Pointer<Int>(pointer); |
| 1411 | |
| 1412 | Int4 vector; |
| 1413 | vector = Insert(vector, x, 0); |
| 1414 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1415 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1417 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1418 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1419 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1420 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1421 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1422 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1423 | Int x = *Pointer<Int>(pointer); |
| 1424 | Int y = *Pointer<Int>(pointer + 4); |
| 1425 | |
| 1426 | Int4 vector; |
| 1427 | vector = Insert(vector, x, 0); |
| 1428 | vector = Insert(vector, y, 1); |
| 1429 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1430 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1431 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1432 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1433 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1434 | else |
| 1435 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1436 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1437 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1438 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1439 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1440 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1441 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1442 | auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1443 | load->addArg(ptr); |
| 1444 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1445 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1446 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1447 | } |
| 1448 | else |
| 1449 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1450 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1451 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1452 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1453 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1454 | return V(result); |
| 1455 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1456 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1457 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1458 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1459 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1460 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1461 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1462 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1463 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1464 | if(align != 0) |
| 1465 | { |
| 1466 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1467 | call->addArg(ptr); |
| 1468 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1469 | ::basicBlock->appendInst(call); |
| 1470 | } |
| 1471 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1472 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1473 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1474 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1475 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1476 | { |
| 1477 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1478 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1479 | if(typeSize(type) == 4) |
| 1480 | { |
| 1481 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1482 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1483 | ::basicBlock->appendInst(bitcast); |
| 1484 | |
| 1485 | RValue<Int4> v(V(vector)); |
| 1486 | |
| 1487 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1488 | Int x = Extract(v, 0); |
| 1489 | *Pointer<Int>(pointer) = x; |
| 1490 | } |
| 1491 | else if(typeSize(type) == 8) |
| 1492 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1493 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1494 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1495 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1496 | ::basicBlock->appendInst(bitcast); |
| 1497 | |
| 1498 | RValue<Int4> v(V(vector)); |
| 1499 | |
| 1500 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1501 | Int x = Extract(v, 0); |
| 1502 | *Pointer<Int>(pointer) = x; |
| 1503 | Int y = Extract(v, 1); |
| 1504 | *Pointer<Int>(pointer + 4) = y; |
| 1505 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1506 | else |
| 1507 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1508 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1509 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1510 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1511 | 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] | 1512 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1513 | auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic); |
| 1514 | store->addArg(value); |
| 1515 | store->addArg(ptr); |
| 1516 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1517 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1518 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1519 | } |
| 1520 | else |
| 1521 | { |
| 1522 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1523 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1524 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1525 | ::basicBlock->appendInst(store); |
| 1526 | } |
| 1527 | |
| 1528 | return value; |
| 1529 | } |
| 1530 | |
| 1531 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1532 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1533 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1534 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1535 | |
| 1536 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1537 | { |
| 1538 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1539 | |
| 1540 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1541 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1542 | return ptr; |
| 1543 | } |
| 1544 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1545 | return createAdd(ptr, createConstantInt(offset)); |
| 1546 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1547 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1548 | if(!Ice::isByteSizedType(T(type))) |
| 1549 | { |
| 1550 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1551 | } |
| 1552 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1553 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1554 | { |
| 1555 | if(unsignedIndex) |
| 1556 | { |
| 1557 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1558 | } |
| 1559 | else |
| 1560 | { |
| 1561 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | return createAdd(ptr, index); |
| 1566 | } |
| 1567 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1568 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1569 | { |
| 1570 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1571 | |
| 1572 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicRMW, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1573 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1574 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1575 | auto op = ::context->getConstantInt32(rmwOp); |
| 1576 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1577 | inst->addArg(op); |
| 1578 | inst->addArg(ptr); |
| 1579 | inst->addArg(value); |
| 1580 | inst->addArg(order); |
| 1581 | ::basicBlock->appendInst(inst); |
| 1582 | |
| 1583 | return V(result); |
| 1584 | } |
| 1585 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1586 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1587 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1588 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1589 | return createAtomicRMW(Ice::Intrinsics::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1593 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1594 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1595 | return createAtomicRMW(Ice::Intrinsics::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1599 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1600 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1601 | return createAtomicRMW(Ice::Intrinsics::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1605 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1606 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1607 | return createAtomicRMW(Ice::Intrinsics::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1611 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1612 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1613 | return createAtomicRMW(Ice::Intrinsics::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1617 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1618 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1619 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1623 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1624 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1625 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1626 | |
| 1627 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicCmpxchg, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1628 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1629 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1630 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1631 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1632 | inst->addArg(ptr); |
| 1633 | inst->addArg(compare); |
| 1634 | inst->addArg(value); |
| 1635 | inst->addArg(orderEq); |
| 1636 | inst->addArg(orderNeq); |
| 1637 | ::basicBlock->appendInst(inst); |
| 1638 | |
| 1639 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1643 | { |
| 1644 | if(v->getType() == T(destType)) |
| 1645 | { |
| 1646 | return v; |
| 1647 | } |
| 1648 | |
| 1649 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1650 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1651 | ::basicBlock->appendInst(cast); |
| 1652 | |
| 1653 | return V(result); |
| 1654 | } |
| 1655 | |
| 1656 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1657 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1658 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1659 | return createCast(Ice::InstCast::Trunc, v, destType); |
| 1660 | } |
| 1661 | |
| 1662 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1663 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1664 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1665 | return createCast(Ice::InstCast::Zext, v, destType); |
| 1666 | } |
| 1667 | |
| 1668 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1669 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1670 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1671 | return createCast(Ice::InstCast::Sext, v, destType); |
| 1672 | } |
| 1673 | |
| 1674 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1675 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1676 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1677 | return createCast(Ice::InstCast::Fptoui, v, destType); |
| 1678 | } |
| 1679 | |
| 1680 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1681 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1682 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1683 | return createCast(Ice::InstCast::Fptosi, v, destType); |
| 1684 | } |
| 1685 | |
| 1686 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1687 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1688 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1689 | return createCast(Ice::InstCast::Sitofp, v, destType); |
| 1690 | } |
| 1691 | |
| 1692 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1693 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1694 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1695 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
| 1696 | } |
| 1697 | |
| 1698 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1699 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1700 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1701 | return createCast(Ice::InstCast::Fpext, v, destType); |
| 1702 | } |
| 1703 | |
| 1704 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1705 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1706 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1707 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1708 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1709 | // emulate them by writing to the stack and reading back as the destination type. |
| 1710 | if(emulateMismatchedBitCast) |
| 1711 | { |
| 1712 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1713 | { |
| 1714 | Value *address = allocateStackVariable(destType); |
| 1715 | createStore(v, address, T(v->getType())); |
| 1716 | return createLoad(address, destType); |
| 1717 | } |
| 1718 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1719 | { |
| 1720 | Value *address = allocateStackVariable(T(v->getType())); |
| 1721 | createStore(v, address, T(v->getType())); |
| 1722 | return createLoad(address, destType); |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1727 | } |
| 1728 | |
| 1729 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1730 | { |
| 1731 | ASSERT(lhs->getType() == rhs->getType()); |
| 1732 | |
| 1733 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1734 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1735 | ::basicBlock->appendInst(cmp); |
| 1736 | |
| 1737 | return V(result); |
| 1738 | } |
| 1739 | |
| 1740 | Value *Nucleus::createPtrEQ(Value *lhs, Value *rhs) |
| 1741 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1742 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1743 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1744 | } |
| 1745 | |
| 1746 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1747 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1748 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1749 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1750 | } |
| 1751 | |
| 1752 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1753 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1754 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1755 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1756 | } |
| 1757 | |
| 1758 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1759 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1760 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1761 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1762 | } |
| 1763 | |
| 1764 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1765 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1766 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1767 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1768 | } |
| 1769 | |
| 1770 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1771 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1772 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1773 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1774 | } |
| 1775 | |
| 1776 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1777 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1778 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1779 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1780 | } |
| 1781 | |
| 1782 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1783 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1784 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1785 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1786 | } |
| 1787 | |
| 1788 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1789 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1790 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1791 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1792 | } |
| 1793 | |
| 1794 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1795 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1796 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1797 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1798 | } |
| 1799 | |
| 1800 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1801 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1802 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1803 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1804 | } |
| 1805 | |
| 1806 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1807 | { |
| 1808 | ASSERT(lhs->getType() == rhs->getType()); |
| 1809 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1810 | |
| 1811 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1812 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1813 | ::basicBlock->appendInst(cmp); |
| 1814 | |
| 1815 | return V(result); |
| 1816 | } |
| 1817 | |
| 1818 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1819 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1820 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1821 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
| 1822 | } |
| 1823 | |
| 1824 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1825 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1826 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1827 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
| 1828 | } |
| 1829 | |
| 1830 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1831 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1832 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1833 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
| 1834 | } |
| 1835 | |
| 1836 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1837 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1838 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1839 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
| 1840 | } |
| 1841 | |
| 1842 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1843 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1844 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1845 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
| 1846 | } |
| 1847 | |
| 1848 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1849 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1850 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1851 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
| 1852 | } |
| 1853 | |
| 1854 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1855 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1856 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1857 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
| 1858 | } |
| 1859 | |
| 1860 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1861 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1862 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1863 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
| 1864 | } |
| 1865 | |
| 1866 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1867 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1868 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1869 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
| 1870 | } |
| 1871 | |
| 1872 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1873 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1874 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1875 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
| 1876 | } |
| 1877 | |
| 1878 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1879 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1880 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1881 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
| 1882 | } |
| 1883 | |
| 1884 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1885 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1886 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1887 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
| 1888 | } |
| 1889 | |
| 1890 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1891 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1892 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1893 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
| 1894 | } |
| 1895 | |
| 1896 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1897 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1898 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1899 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1900 | } |
| 1901 | |
| 1902 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1903 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1904 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1905 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1906 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1907 | ::basicBlock->appendInst(extract); |
| 1908 | |
| 1909 | return V(result); |
| 1910 | } |
| 1911 | |
| 1912 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1913 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1914 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1915 | auto result = ::function->makeVariable(vector->getType()); |
| 1916 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1917 | ::basicBlock->appendInst(insert); |
| 1918 | |
| 1919 | return V(result); |
| 1920 | } |
| 1921 | |
| 1922 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 1923 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1924 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1925 | ASSERT(V1->getType() == V2->getType()); |
| 1926 | |
| 1927 | int size = Ice::typeNumElements(V1->getType()); |
| 1928 | auto result = ::function->makeVariable(V1->getType()); |
| 1929 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1930 | |
| 1931 | for(int i = 0; i < size; i++) |
| 1932 | { |
| 1933 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1934 | } |
| 1935 | |
| 1936 | ::basicBlock->appendInst(shuffle); |
| 1937 | |
| 1938 | return V(result); |
| 1939 | } |
| 1940 | |
| 1941 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1942 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1943 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1944 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1945 | |
| 1946 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1947 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1948 | ::basicBlock->appendInst(select); |
| 1949 | |
| 1950 | return V(result); |
| 1951 | } |
| 1952 | |
| 1953 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1954 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1955 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1956 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1957 | ::basicBlock->appendInst(switchInst); |
| 1958 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1959 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1963 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1964 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1965 | switchCases->addBranch(label, label, branch); |
| 1966 | } |
| 1967 | |
| 1968 | void Nucleus::createUnreachable() |
| 1969 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1970 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1971 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1972 | ::basicBlock->appendInst(unreachable); |
| 1973 | } |
| 1974 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1975 | Type *Nucleus::getType(Value *value) |
| 1976 | { |
| 1977 | return T(V(value)->getType()); |
| 1978 | } |
| 1979 | |
| 1980 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1981 | { |
| 1982 | Ice::Type vecTy = T(vectorType); |
| 1983 | switch(vecTy) |
| 1984 | { |
| 1985 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1986 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1987 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1988 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1989 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1990 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1991 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1992 | default: |
| 1993 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 1994 | return {}; |
| 1995 | } |
| 1996 | } |
| 1997 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1998 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1999 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 2000 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2001 | } |
| 2002 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2003 | static constexpr Ice::Type getNaturalIntType() |
| 2004 | { |
| 2005 | constexpr size_t intSize = sizeof(int); |
| 2006 | static_assert(intSize == 4 || intSize == 8, ""); |
| 2007 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 2008 | } |
| 2009 | |
| 2010 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 2011 | { |
| 2012 | Ice::Type valueTy = T(valueType); |
| 2013 | switch(valueTy) |
| 2014 | { |
| 2015 | case Ice::IceType_i32: |
| 2016 | return T(getNaturalIntType()); |
| 2017 | |
| 2018 | case Ice::IceType_f32: |
| 2019 | return T(Ice::IceType_f64); |
| 2020 | |
| 2021 | default: |
| 2022 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 2023 | return {}; |
| 2024 | } |
| 2025 | } |
| 2026 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2027 | Value *Nucleus::createNullValue(Type *Ty) |
| 2028 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2029 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2030 | if(Ice::isVectorType(T(Ty))) |
| 2031 | { |
| 2032 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2033 | 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] | 2034 | return createConstantVector(c, Ty); |
| 2035 | } |
| 2036 | else |
| 2037 | { |
| 2038 | return V(::context->getConstantZero(T(Ty))); |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | Value *Nucleus::createConstantLong(int64_t i) |
| 2043 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2044 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2045 | return V(::context->getConstantInt64(i)); |
| 2046 | } |
| 2047 | |
| 2048 | Value *Nucleus::createConstantInt(int i) |
| 2049 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2050 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2051 | return V(::context->getConstantInt32(i)); |
| 2052 | } |
| 2053 | |
| 2054 | Value *Nucleus::createConstantInt(unsigned int i) |
| 2055 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2056 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2057 | return V(::context->getConstantInt32(i)); |
| 2058 | } |
| 2059 | |
| 2060 | Value *Nucleus::createConstantBool(bool b) |
| 2061 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2062 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2063 | return V(::context->getConstantInt1(b)); |
| 2064 | } |
| 2065 | |
| 2066 | Value *Nucleus::createConstantByte(signed char i) |
| 2067 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2068 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2069 | return V(::context->getConstantInt8(i)); |
| 2070 | } |
| 2071 | |
| 2072 | Value *Nucleus::createConstantByte(unsigned char i) |
| 2073 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2074 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2075 | return V(::context->getConstantInt8(i)); |
| 2076 | } |
| 2077 | |
| 2078 | Value *Nucleus::createConstantShort(short i) |
| 2079 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2080 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2081 | return V(::context->getConstantInt16(i)); |
| 2082 | } |
| 2083 | |
| 2084 | Value *Nucleus::createConstantShort(unsigned short i) |
| 2085 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2086 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2087 | return V(::context->getConstantInt16(i)); |
| 2088 | } |
| 2089 | |
| 2090 | Value *Nucleus::createConstantFloat(float x) |
| 2091 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2092 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2093 | return V(::context->getConstantFloat(x)); |
| 2094 | } |
| 2095 | |
| 2096 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2097 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2098 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2099 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2100 | } |
| 2101 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2102 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2103 | { |
| 2104 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2105 | } |
| 2106 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2107 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2108 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2109 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2110 | const int vectorSize = 16; |
| 2111 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2112 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2113 | |
| 2114 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2115 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2116 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2117 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2118 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2119 | |
| 2120 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2121 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2122 | case Ice::IceType_v4i32: |
| 2123 | case Ice::IceType_v4i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2124 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2125 | 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] | 2126 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2127 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2128 | } |
| 2129 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2130 | case Ice::IceType_v4f32: |
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 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] | 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_v8i16: |
| 2138 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2139 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2140 | 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] | 2141 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2142 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2143 | } |
| 2144 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2145 | case Ice::IceType_v16i8: |
| 2146 | case Ice::IceType_v16i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2147 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2148 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[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] | 2149 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2150 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2151 | } |
| 2152 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2153 | case Type_v2i32: |
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 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] | 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_v2f32: |
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 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] | 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_v4i16: |
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 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] | 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_v8i8: |
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 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] | 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_v4i8: |
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[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] | 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 | default: |
| 2189 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2190 | } |
| 2191 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2192 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2193 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2194 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2195 | return V(result); |
| 2196 | } |
| 2197 | |
| 2198 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2199 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2200 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2201 | } |
| 2202 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2203 | Value *Nucleus::createConstantString(const char *v) |
| 2204 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2205 | // 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] | 2206 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2207 | } |
| 2208 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2209 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2210 | { |
| 2211 | return T(Ice::IceType_void); |
| 2212 | } |
| 2213 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2214 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2215 | { |
| 2216 | return T(Ice::IceType_i1); |
| 2217 | } |
| 2218 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2219 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2220 | { |
| 2221 | return T(Ice::IceType_i8); |
| 2222 | } |
| 2223 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2224 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2225 | { |
| 2226 | return T(Ice::IceType_i8); |
| 2227 | } |
| 2228 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2229 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2230 | { |
| 2231 | return T(Ice::IceType_i16); |
| 2232 | } |
| 2233 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2234 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2235 | { |
| 2236 | return T(Ice::IceType_i16); |
| 2237 | } |
| 2238 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2239 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2240 | { |
| 2241 | return T(Type_v4i8); |
| 2242 | } |
| 2243 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2244 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2245 | { |
| 2246 | return T(Type_v4i8); |
| 2247 | } |
| 2248 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2249 | namespace { |
| 2250 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2251 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2252 | 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] | 2253 | } |
| 2254 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2255 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2256 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2257 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2261 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2262 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2263 | } |
| 2264 | } // namespace |
| 2265 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2266 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2267 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2268 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2269 | if(emulateIntrinsics) |
| 2270 | { |
| 2271 | Byte8 result; |
| 2272 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2273 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2274 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2275 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2276 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2277 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2278 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2279 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2280 | |
| 2281 | return result; |
| 2282 | } |
| 2283 | else |
| 2284 | { |
| 2285 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2286 | 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] | 2287 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2288 | auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2289 | paddusb->addArg(x.value()); |
| 2290 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2291 | ::basicBlock->appendInst(paddusb); |
| 2292 | |
| 2293 | return RValue<Byte8>(V(result)); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2298 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2299 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2300 | if(emulateIntrinsics) |
| 2301 | { |
| 2302 | Byte8 result; |
| 2303 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2304 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2305 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2306 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2307 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2308 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2309 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2310 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2311 | |
| 2312 | return result; |
| 2313 | } |
| 2314 | else |
| 2315 | { |
| 2316 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2317 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2318 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2319 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2320 | psubusw->addArg(x.value()); |
| 2321 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2322 | ::basicBlock->appendInst(psubusw); |
| 2323 | |
| 2324 | return RValue<Byte8>(V(result)); |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 2329 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2330 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2331 | return RValue<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2335 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2337 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2343 | if(emulateIntrinsics) |
| 2344 | { |
| 2345 | SByte8 result; |
| 2346 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2347 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2348 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2349 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2350 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2351 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2352 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2353 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2354 | |
| 2355 | return result; |
| 2356 | } |
| 2357 | else |
| 2358 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2359 | #if defined(__i386__) || defined(__x86_64__) |
| 2360 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2361 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2362 | 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] | 2363 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2364 | return As<SByte8>(hi | lo); |
| 2365 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2366 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2367 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2368 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2369 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2370 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2371 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2372 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2373 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2374 | if(emulateIntrinsics || CPUID::ARM) |
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 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2377 | 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] | 2378 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2379 | else |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2380 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2381 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2382 | 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] | 2383 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2384 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2385 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2386 | ::basicBlock->appendInst(movmsk); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2387 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2388 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2389 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2390 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2391 | |
| 2392 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2393 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2394 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2395 | // } |
| 2396 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2397 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2398 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2399 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2400 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2401 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2402 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2403 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2404 | { |
| 2405 | return T(Type_v8i8); |
| 2406 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2407 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2408 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2409 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2410 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2411 | // } |
| 2412 | |
| 2413 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2414 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2415 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2416 | // } |
| 2417 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2418 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 2419 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2421 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2422 | } |
| 2423 | |
| 2424 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2427 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2428 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2429 | SByte8 result; |
| 2430 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2431 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2432 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2433 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2434 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2435 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2436 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2437 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2438 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2439 | return result; |
| 2440 | } |
| 2441 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2442 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2443 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2444 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2445 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2446 | auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2447 | paddsb->addArg(x.value()); |
| 2448 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2449 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2450 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2451 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2452 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2453 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2454 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2455 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2456 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2457 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2458 | if(emulateIntrinsics) |
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 | SByte8 result; |
| 2461 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2462 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2463 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2464 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2465 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2466 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2467 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2468 | 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] | 2469 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2470 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2471 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2472 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2473 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2474 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2475 | 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] | 2476 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2477 | auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2478 | psubsb->addArg(x.value()); |
| 2479 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2480 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2481 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2482 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2483 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2484 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2485 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2486 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2487 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2488 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2489 | if(emulateIntrinsics || CPUID::ARM) |
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 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2492 | 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] | 2493 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2494 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2495 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2496 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2497 | 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] | 2498 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2499 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2500 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2501 | ::basicBlock->appendInst(movmsk); |
| 2502 | |
| 2503 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2504 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2505 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2506 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2507 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 2508 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2509 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2510 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2511 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2512 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2513 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2514 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2515 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2516 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2517 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2518 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2519 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2520 | { |
| 2521 | return T(Type_v8i8); |
| 2522 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2523 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2524 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2525 | { |
| 2526 | return T(Ice::IceType_v16i8); |
| 2527 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2528 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2529 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2530 | { |
| 2531 | return T(Ice::IceType_v16i8); |
| 2532 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2533 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2534 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2535 | { |
| 2536 | return T(Type_v2i16); |
| 2537 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2538 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2539 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2540 | { |
| 2541 | return T(Type_v2i16); |
| 2542 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2543 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2544 | Short4::Short4(RValue<Int4> cast) |
| 2545 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2546 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2547 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2548 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2549 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2550 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2551 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2552 | |
| 2553 | storeValue(short4); |
| 2554 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2555 | |
| 2556 | // Short4::Short4(RValue<Float> cast) |
| 2557 | // { |
| 2558 | // } |
| 2559 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2560 | Short4::Short4(RValue<Float4> cast) |
| 2561 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2562 | // TODO(b/150791192): Generalize and optimize |
| 2563 | auto smin = std::numeric_limits<short>::min(); |
| 2564 | auto smax = std::numeric_limits<short>::max(); |
| 2565 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2569 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2570 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2571 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2572 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2573 | Short4 result; |
| 2574 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2575 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2576 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2577 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2578 | |
| 2579 | return result; |
| 2580 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2581 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2582 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2583 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2584 | } |
| 2585 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2586 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2587 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2588 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2589 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2590 | if(emulateIntrinsics) |
| 2591 | { |
| 2592 | Short4 result; |
| 2593 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2594 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2595 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2596 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2597 | |
| 2598 | return result; |
| 2599 | } |
| 2600 | else |
| 2601 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2602 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2607 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2608 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2609 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2610 | 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] | 2611 | ::basicBlock->appendInst(cmp); |
| 2612 | |
| 2613 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2614 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2615 | ::basicBlock->appendInst(select); |
| 2616 | |
| 2617 | return RValue<Short4>(V(result)); |
| 2618 | } |
| 2619 | |
| 2620 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2621 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2622 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2623 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2624 | 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] | 2625 | ::basicBlock->appendInst(cmp); |
| 2626 | |
| 2627 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2628 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2629 | ::basicBlock->appendInst(select); |
| 2630 | |
| 2631 | return RValue<Short4>(V(result)); |
| 2632 | } |
| 2633 | |
| 2634 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 2635 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2636 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2637 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2638 | } |
| 2639 | |
| 2640 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2641 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2642 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2643 | if(emulateIntrinsics) |
| 2644 | { |
| 2645 | Short4 result; |
| 2646 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2647 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2648 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2649 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2650 | |
| 2651 | return result; |
| 2652 | } |
| 2653 | else |
| 2654 | { |
| 2655 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2656 | 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] | 2657 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2658 | auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2659 | paddsw->addArg(x.value()); |
| 2660 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2661 | ::basicBlock->appendInst(paddsw); |
| 2662 | |
| 2663 | return RValue<Short4>(V(result)); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2668 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2669 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2670 | if(emulateIntrinsics) |
| 2671 | { |
| 2672 | Short4 result; |
| 2673 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2674 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2675 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2676 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2677 | |
| 2678 | return result; |
| 2679 | } |
| 2680 | else |
| 2681 | { |
| 2682 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2683 | 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] | 2684 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2685 | auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2686 | psubsw->addArg(x.value()); |
| 2687 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2688 | ::basicBlock->appendInst(psubsw); |
| 2689 | |
| 2690 | return RValue<Short4>(V(result)); |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2695 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2696 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2697 | if(emulateIntrinsics) |
| 2698 | { |
| 2699 | Short4 result; |
| 2700 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2701 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2702 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2703 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2704 | |
| 2705 | return result; |
| 2706 | } |
| 2707 | else |
| 2708 | { |
| 2709 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2710 | 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] | 2711 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2712 | auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2713 | pmulhw->addArg(x.value()); |
| 2714 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2715 | ::basicBlock->appendInst(pmulhw); |
| 2716 | |
| 2717 | return RValue<Short4>(V(result)); |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2722 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2723 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2724 | if(emulateIntrinsics) |
| 2725 | { |
| 2726 | Int2 result; |
| 2727 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2728 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2729 | |
| 2730 | return result; |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2735 | 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] | 2736 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2737 | auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2738 | pmaddwd->addArg(x.value()); |
| 2739 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2740 | ::basicBlock->appendInst(pmaddwd); |
| 2741 | |
| 2742 | return As<Int2>(V(result)); |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2747 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2748 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2749 | if(emulateIntrinsics) |
| 2750 | { |
| 2751 | SByte8 result; |
| 2752 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2753 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2754 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2755 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2756 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2757 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2758 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2759 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2760 | |
| 2761 | return result; |
| 2762 | } |
| 2763 | else |
| 2764 | { |
| 2765 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2766 | 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] | 2767 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2768 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2769 | pack->addArg(x.value()); |
| 2770 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2771 | ::basicBlock->appendInst(pack); |
| 2772 | |
| 2773 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2778 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2779 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2780 | if(emulateIntrinsics) |
| 2781 | { |
| 2782 | Byte8 result; |
| 2783 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2784 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2785 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2786 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2787 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2788 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2789 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2790 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2791 | |
| 2792 | return result; |
| 2793 | } |
| 2794 | else |
| 2795 | { |
| 2796 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2797 | 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] | 2798 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2799 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2800 | pack->addArg(x.value()); |
| 2801 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2802 | ::basicBlock->appendInst(pack); |
| 2803 | |
| 2804 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2809 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2810 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2811 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2815 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2816 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2817 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2818 | } |
| 2819 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2820 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2821 | { |
| 2822 | return T(Type_v4i16); |
| 2823 | } |
| 2824 | |
| 2825 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2826 | { |
| 2827 | if(saturate) |
| 2828 | { |
| 2829 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2830 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2831 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2832 | // PackUnsigned takes care of 0x0000 saturation. |
| 2833 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2834 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2835 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2836 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2837 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2838 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2839 | Int4 int4(cast); |
| 2840 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2841 | } |
| 2842 | else |
| 2843 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2844 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2845 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2846 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2847 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2848 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2849 | *this = Short4(Int4(cast)); |
| 2850 | } |
| 2851 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2852 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2853 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2854 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2855 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2859 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2860 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2864 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2865 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2866 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2867 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2868 | { |
| 2869 | UShort4 result; |
| 2870 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2871 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2872 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2873 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2874 | |
| 2875 | return result; |
| 2876 | } |
| 2877 | else |
| 2878 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2879 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2884 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2885 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2886 | if(emulateIntrinsics) |
| 2887 | { |
| 2888 | UShort4 result; |
| 2889 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2890 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2891 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2892 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2893 | |
| 2894 | return result; |
| 2895 | } |
| 2896 | else |
| 2897 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2898 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2903 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2904 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2905 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2906 | 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] | 2907 | ::basicBlock->appendInst(cmp); |
| 2908 | |
| 2909 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2910 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2911 | ::basicBlock->appendInst(select); |
| 2912 | |
| 2913 | return RValue<UShort4>(V(result)); |
| 2914 | } |
| 2915 | |
| 2916 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2917 | { |
| 2918 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2919 | 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] | 2920 | ::basicBlock->appendInst(cmp); |
| 2921 | |
| 2922 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2923 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2924 | ::basicBlock->appendInst(select); |
| 2925 | |
| 2926 | return RValue<UShort4>(V(result)); |
| 2927 | } |
| 2928 | |
| 2929 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 2930 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2931 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2932 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2933 | } |
| 2934 | |
| 2935 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2936 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2937 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2938 | if(emulateIntrinsics) |
| 2939 | { |
| 2940 | UShort4 result; |
| 2941 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2942 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2943 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2944 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2945 | |
| 2946 | return result; |
| 2947 | } |
| 2948 | else |
| 2949 | { |
| 2950 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2951 | 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] | 2952 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2953 | auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2954 | paddusw->addArg(x.value()); |
| 2955 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2956 | ::basicBlock->appendInst(paddusw); |
| 2957 | |
| 2958 | return RValue<UShort4>(V(result)); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2963 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2964 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2965 | if(emulateIntrinsics) |
| 2966 | { |
| 2967 | UShort4 result; |
| 2968 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2969 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2970 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2971 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2972 | |
| 2973 | return result; |
| 2974 | } |
| 2975 | else |
| 2976 | { |
| 2977 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2978 | 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] | 2979 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2980 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2981 | psubusw->addArg(x.value()); |
| 2982 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2983 | ::basicBlock->appendInst(psubusw); |
| 2984 | |
| 2985 | return RValue<UShort4>(V(result)); |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2990 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2991 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2992 | if(emulateIntrinsics) |
| 2993 | { |
| 2994 | UShort4 result; |
| 2995 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2996 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2997 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2998 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 2999 | |
| 3000 | return result; |
| 3001 | } |
| 3002 | else |
| 3003 | { |
| 3004 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3005 | 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] | 3006 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3007 | auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3008 | pmulhuw->addArg(x.value()); |
| 3009 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3010 | ::basicBlock->appendInst(pmulhuw); |
| 3011 | |
| 3012 | return RValue<UShort4>(V(result)); |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3017 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3018 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3019 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3020 | |
| 3021 | // Scalarized implementation. |
| 3022 | Int4 result; |
| 3023 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 3024 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 3025 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 3026 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 3027 | |
| 3028 | return result; |
| 3029 | } |
| 3030 | |
| 3031 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3032 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3033 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3034 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3035 | |
| 3036 | if(false) // Partial product based implementation. |
| 3037 | { |
| 3038 | auto xh = x >> 16; |
| 3039 | auto yh = y >> 16; |
| 3040 | auto xl = x & UInt4(0x0000FFFF); |
| 3041 | auto yl = y & UInt4(0x0000FFFF); |
| 3042 | auto xlyh = xl * yh; |
| 3043 | auto xhyl = xh * yl; |
| 3044 | auto xlyhh = xlyh >> 16; |
| 3045 | auto xhylh = xhyl >> 16; |
| 3046 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3047 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3048 | auto xlylh = (xl * yl) >> 16; |
| 3049 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3050 | |
| 3051 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3052 | } |
| 3053 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3054 | // Scalarized implementation. |
| 3055 | Int4 result; |
| 3056 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3057 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3058 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3059 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3060 | |
| 3061 | return As<UInt4>(result); |
| 3062 | } |
| 3063 | |
| 3064 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3065 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3066 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3067 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3068 | return UShort4(0); |
| 3069 | } |
| 3070 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3071 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3072 | { |
| 3073 | return T(Type_v4i16); |
| 3074 | } |
| 3075 | |
| 3076 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 3077 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3078 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3079 | return RValue<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3083 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3084 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3085 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3089 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3090 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3091 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3092 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3093 | Short8 result; |
| 3094 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3095 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3096 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3097 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3098 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3099 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3100 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3101 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3103 | return result; |
| 3104 | } |
| 3105 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3106 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3107 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3108 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3109 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3110 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3111 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 3112 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3113 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3114 | if(emulateIntrinsics) |
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 | Short8 result; |
| 3117 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 3118 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 3119 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 3120 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 3121 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 3122 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 3123 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 3124 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3125 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3126 | return result; |
| 3127 | } |
| 3128 | else |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3129 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3130 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3131 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3132 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3133 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3134 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3135 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3136 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3137 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3138 | return Int4(0); |
| 3139 | } |
| 3140 | |
| 3141 | RValue<Short8> MulHigh(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<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3145 | return Short8(0); |
| 3146 | } |
| 3147 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3148 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3149 | { |
| 3150 | return T(Ice::IceType_v8i16); |
| 3151 | } |
| 3152 | |
| 3153 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 3154 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3155 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3156 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3160 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3161 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3162 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3166 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3167 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3168 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3169 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3170 | UShort8 result; |
| 3171 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3172 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3173 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3174 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3175 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3176 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3177 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3178 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3179 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3180 | return result; |
| 3181 | } |
| 3182 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3183 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3184 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3185 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3186 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3187 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3188 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 3189 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3190 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3191 | if(emulateIntrinsics) |
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 | UShort8 result; |
| 3194 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 3195 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 3196 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 3197 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 3198 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 3199 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 3200 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 3201 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3202 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3203 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3204 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3205 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3206 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3207 | return RValue<UShort8>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3208 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3209 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3210 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3211 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3212 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3213 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3214 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3215 | return UShort8(0); |
| 3216 | } |
| 3217 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3218 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3219 | { |
| 3220 | return T(Ice::IceType_v8i16); |
| 3221 | } |
| 3222 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3223 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3224 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3225 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3226 | RValue<Int> res = val; |
| 3227 | val += 1; |
| 3228 | return res; |
| 3229 | } |
| 3230 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3231 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3232 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3233 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3234 | val += 1; |
| 3235 | return val; |
| 3236 | } |
| 3237 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3238 | RValue<Int> operator--(Int &val, int) // Post-decrement |
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 | RValue<Int> res = val; |
| 3242 | val -= 1; |
| 3243 | return res; |
| 3244 | } |
| 3245 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3246 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3247 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3248 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3249 | val -= 1; |
| 3250 | return val; |
| 3251 | } |
| 3252 | |
| 3253 | RValue<Int> RoundInt(RValue<Float> cast) |
| 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 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3257 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3258 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3259 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3260 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3261 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3262 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3263 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3264 | 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] | 3265 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3266 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3267 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3268 | ::basicBlock->appendInst(nearbyint); |
| 3269 | |
| 3270 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3271 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3272 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3273 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3274 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3275 | { |
| 3276 | return T(Ice::IceType_i32); |
| 3277 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3278 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3279 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3280 | { |
| 3281 | return T(Ice::IceType_i64); |
| 3282 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3283 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3284 | UInt::UInt(RValue<Float> cast) |
| 3285 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3286 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3287 | // Smallest positive value representable in UInt, but not in Int |
| 3288 | const unsigned int ustart = 0x80000000u; |
| 3289 | const float ustartf = float(ustart); |
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 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3292 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3293 | // Check if the value can be represented as an Int |
| 3294 | IfThenElse(cast >= ustartf, |
| 3295 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3296 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3297 | // Otherwise, just convert normally |
| 3298 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3299 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3300 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3301 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3302 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3305 | RValue<UInt> res = val; |
| 3306 | val += 1; |
| 3307 | return res; |
| 3308 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3309 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3310 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3311 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3313 | val += 1; |
| 3314 | return val; |
| 3315 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3316 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3317 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
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 | RValue<UInt> res = val; |
| 3321 | val -= 1; |
| 3322 | return res; |
| 3323 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3324 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3325 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3326 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3327 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3328 | val -= 1; |
| 3329 | return val; |
| 3330 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3331 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3332 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3333 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3334 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3335 | // } |
| 3336 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3337 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3338 | { |
| 3339 | return T(Ice::IceType_i32); |
| 3340 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3341 | |
| 3342 | // Int2::Int2(RValue<Int> cast) |
| 3343 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3344 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3345 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3346 | // |
| 3347 | // Constant *shuffle[2]; |
| 3348 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3349 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3350 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3351 | // 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] | 3352 | // |
| 3353 | // storeValue(replicate); |
| 3354 | // } |
| 3355 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3356 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3357 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3358 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3359 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3360 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3361 | Int2 result; |
| 3362 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3363 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3364 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3365 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3366 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3367 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3368 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3369 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3370 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3371 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3372 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3373 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3374 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3375 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3376 | if(emulateIntrinsics) |
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 | Int2 result; |
| 3379 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3380 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3381 | |
| 3382 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3383 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3384 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3385 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3386 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3387 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3388 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3389 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3390 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3391 | { |
| 3392 | return T(Type_v2i32); |
| 3393 | } |
| 3394 | |
| 3395 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 3396 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3397 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3398 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3399 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3400 | UInt2 result; |
| 3401 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3402 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3403 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3404 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3405 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3406 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3407 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3408 | return RValue<UInt2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3409 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3410 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3411 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3412 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3413 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3415 | if(emulateIntrinsics) |
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 | UInt2 result; |
| 3418 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3419 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3420 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3421 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3422 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3423 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3424 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3425 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3426 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3427 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3428 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3429 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3430 | { |
| 3431 | return T(Type_v2i32); |
| 3432 | } |
| 3433 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3434 | Int4::Int4(RValue<Byte4> cast) |
| 3435 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3436 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3437 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3438 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3439 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3440 | |
| 3441 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3442 | 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] | 3443 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3444 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3445 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3446 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3447 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3448 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3449 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3450 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3451 | storeValue(f); |
| 3452 | } |
| 3453 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3454 | Int4::Int4(RValue<SByte4> cast) |
| 3455 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3456 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3457 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3458 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3459 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3460 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3461 | 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] | 3462 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3463 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3464 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3465 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3466 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3467 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3468 | |
| 3469 | *this = As<Int4>(e) >> 24; |
| 3470 | } |
| 3471 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3472 | Int4::Int4(RValue<Short4> cast) |
| 3473 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3474 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3475 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3476 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3477 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3478 | |
| 3479 | *this = As<Int4>(c) >> 16; |
| 3480 | } |
| 3481 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3482 | Int4::Int4(RValue<UShort4> cast) |
| 3483 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3484 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3485 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3486 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3487 | 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] | 3488 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3489 | storeValue(d); |
| 3490 | } |
| 3491 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3492 | Int4::Int4(RValue<Int> rhs) |
| 3493 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3494 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3495 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3496 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3497 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3498 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3499 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3500 | |
| 3501 | storeValue(replicate); |
| 3502 | } |
| 3503 | |
| 3504 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3505 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3506 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3507 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3508 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3509 | Int4 result; |
| 3510 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3511 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3512 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3513 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3514 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3515 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3516 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3517 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3518 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3519 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3520 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3521 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3522 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3523 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3524 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3525 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3526 | if(emulateIntrinsics) |
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 | Int4 result; |
| 3529 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3530 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3531 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3532 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3533 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3534 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3535 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3536 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3537 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3538 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3539 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3540 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3541 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3542 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3543 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3544 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3545 | return RValue<Int4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3549 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3550 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3551 | return RValue<Int4>(Nucleus::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3555 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3556 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3557 | return RValue<Int4>(Nucleus::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3561 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3562 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3563 | return RValue<Int4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3567 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3568 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3569 | return RValue<Int4>(Nucleus::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3573 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3574 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3575 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3579 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3580 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3581 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3582 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3583 | ::basicBlock->appendInst(cmp); |
| 3584 | |
| 3585 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3586 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3587 | ::basicBlock->appendInst(select); |
| 3588 | |
| 3589 | return RValue<Int4>(V(result)); |
| 3590 | } |
| 3591 | |
| 3592 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3593 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3594 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3595 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3596 | 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] | 3597 | ::basicBlock->appendInst(cmp); |
| 3598 | |
| 3599 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3600 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3601 | ::basicBlock->appendInst(select); |
| 3602 | |
| 3603 | return RValue<Int4>(V(result)); |
| 3604 | } |
| 3605 | |
| 3606 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3607 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3608 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3609 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3610 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3611 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3612 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3613 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3614 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3615 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3616 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3617 | 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] | 3618 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3619 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3620 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3621 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3622 | |
| 3623 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3624 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3625 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3626 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3627 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3628 | { |
| 3629 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3630 | |
| 3631 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3632 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3633 | // saturate to 0x80000000. |
| 3634 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3635 | |
| 3636 | if(emulateIntrinsics || CPUID::ARM) |
| 3637 | { |
| 3638 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3639 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3640 | } |
| 3641 | else |
| 3642 | { |
| 3643 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3644 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 3645 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3646 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3647 | nearbyint->addArg(clamped.value()); |
| 3648 | ::basicBlock->appendInst(nearbyint); |
| 3649 | |
| 3650 | return RValue<Int4>(V(result)); |
| 3651 | } |
| 3652 | } |
| 3653 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3654 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3655 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3656 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3657 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3658 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3659 | Short8 result; |
| 3660 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3661 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3662 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3663 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3664 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3665 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3666 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3667 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3668 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3669 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3670 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3671 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3672 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3673 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3674 | 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] | 3675 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3676 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3677 | pack->addArg(x.value()); |
| 3678 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3679 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3680 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3681 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3682 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3683 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3684 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3685 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3686 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3687 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3688 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
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 | RValue<Int4> sx = As<Int4>(x); |
| 3691 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3692 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3693 | RValue<Int4> sy = As<Int4>(y); |
| 3694 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3695 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3696 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3697 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3698 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3699 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3700 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3701 | 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] | 3702 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3703 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3704 | pack->addArg(x.value()); |
| 3705 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3706 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3707 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3708 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3709 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3710 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3711 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3712 | RValue<Int> SignMask(RValue<Int4> x) |
| 3713 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3714 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3715 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3716 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3717 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3718 | 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] | 3719 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3720 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3721 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3722 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3723 | 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] | 3724 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3725 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3726 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3727 | ::basicBlock->appendInst(movmsk); |
| 3728 | |
| 3729 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3730 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3731 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3732 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3733 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3734 | { |
| 3735 | return T(Ice::IceType_v4i32); |
| 3736 | } |
| 3737 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3738 | UInt4::UInt4(RValue<Float4> cast) |
| 3739 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3740 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3741 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3742 | // Smallest positive value representable in UInt, but not in Int |
| 3743 | const unsigned int ustart = 0x80000000u; |
| 3744 | const float ustartf = float(ustart); |
| 3745 | |
| 3746 | // Check if the value can be represented as an Int |
| 3747 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3748 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3749 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3750 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3751 | (~uiValue & Int4(cast)); |
| 3752 | // 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] | 3753 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3754 | } |
| 3755 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3756 | UInt4::UInt4(RValue<UInt> rhs) |
| 3757 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3758 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3759 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3760 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3761 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3762 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3763 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3764 | |
| 3765 | storeValue(replicate); |
| 3766 | } |
| 3767 | |
| 3768 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3769 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3770 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3771 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3772 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3773 | UInt4 result; |
| 3774 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3775 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3776 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3777 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3778 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3779 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3780 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3781 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3782 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3783 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3784 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3785 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3786 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3787 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3788 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3789 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3790 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3791 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3792 | UInt4 result; |
| 3793 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3794 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3795 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3796 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3797 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3798 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3799 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3800 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3801 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3802 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3803 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3804 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3805 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3806 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3807 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3808 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3809 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3813 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3814 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3815 | return RValue<UInt4>(Nucleus::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3819 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3820 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3821 | return RValue<UInt4>(Nucleus::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3825 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3826 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3827 | return RValue<UInt4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3831 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3832 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3833 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3837 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3838 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3839 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3843 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3844 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3845 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3846 | 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] | 3847 | ::basicBlock->appendInst(cmp); |
| 3848 | |
| 3849 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3850 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3851 | ::basicBlock->appendInst(select); |
| 3852 | |
| 3853 | return RValue<UInt4>(V(result)); |
| 3854 | } |
| 3855 | |
| 3856 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3857 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3858 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3859 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3860 | 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] | 3861 | ::basicBlock->appendInst(cmp); |
| 3862 | |
| 3863 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3864 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3865 | ::basicBlock->appendInst(select); |
| 3866 | |
| 3867 | return RValue<UInt4>(V(result)); |
| 3868 | } |
| 3869 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3870 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3871 | { |
| 3872 | return T(Ice::IceType_v4i32); |
| 3873 | } |
| 3874 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3875 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3876 | { |
| 3877 | return T(Ice::IceType_i16); |
| 3878 | } |
| 3879 | |
| 3880 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3881 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3882 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3883 | return 1.0f / x; |
| 3884 | } |
| 3885 | |
| 3886 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3887 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3888 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3889 | return Rcp_pp(Sqrt(x)); |
| 3890 | } |
| 3891 | |
| 3892 | RValue<Float> Sqrt(RValue<Float> x) |
| 3893 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3894 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3895 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3896 | 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] | 3897 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3898 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3899 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3900 | ::basicBlock->appendInst(sqrt); |
| 3901 | |
| 3902 | return RValue<Float>(V(result)); |
| 3903 | } |
| 3904 | |
| 3905 | RValue<Float> Round(RValue<Float> x) |
| 3906 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3907 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3908 | return Float4(Round(Float4(x))).x; |
| 3909 | } |
| 3910 | |
| 3911 | RValue<Float> Trunc(RValue<Float> x) |
| 3912 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3913 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3914 | return Float4(Trunc(Float4(x))).x; |
| 3915 | } |
| 3916 | |
| 3917 | RValue<Float> Frac(RValue<Float> x) |
| 3918 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3919 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3920 | return Float4(Frac(Float4(x))).x; |
| 3921 | } |
| 3922 | |
| 3923 | RValue<Float> Floor(RValue<Float> x) |
| 3924 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3925 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3926 | return Float4(Floor(Float4(x))).x; |
| 3927 | } |
| 3928 | |
| 3929 | RValue<Float> Ceil(RValue<Float> x) |
| 3930 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3931 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3932 | return Float4(Ceil(Float4(x))).x; |
| 3933 | } |
| 3934 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3935 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3936 | { |
| 3937 | return T(Ice::IceType_f32); |
| 3938 | } |
| 3939 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3940 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3941 | { |
| 3942 | return T(Type_v2f32); |
| 3943 | } |
| 3944 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3945 | Float4::Float4(RValue<Float> rhs) |
| 3946 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3947 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3948 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3949 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3950 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3951 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3952 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3953 | |
| 3954 | storeValue(replicate); |
| 3955 | } |
| 3956 | |
| 3957 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3958 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3959 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3960 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3961 | 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] | 3962 | ::basicBlock->appendInst(cmp); |
| 3963 | |
| 3964 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3965 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3966 | ::basicBlock->appendInst(select); |
| 3967 | |
| 3968 | return RValue<Float4>(V(result)); |
| 3969 | } |
| 3970 | |
| 3971 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3972 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3973 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3974 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3975 | 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] | 3976 | ::basicBlock->appendInst(cmp); |
| 3977 | |
| 3978 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3979 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3980 | ::basicBlock->appendInst(select); |
| 3981 | |
| 3982 | return RValue<Float4>(V(result)); |
| 3983 | } |
| 3984 | |
| 3985 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3986 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3987 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3988 | return Float4(1.0f) / x; |
| 3989 | } |
| 3990 | |
| 3991 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3992 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3993 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3994 | return Rcp_pp(Sqrt(x)); |
| 3995 | } |
| 3996 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 3997 | bool HasRcpApprox() |
| 3998 | { |
| 3999 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4000 | return false; |
| 4001 | } |
| 4002 | |
| 4003 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 4004 | { |
| 4005 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4006 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 4007 | return { 0.0f }; |
| 4008 | } |
| 4009 | |
| 4010 | RValue<Float> RcpApprox(RValue<Float> 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<Float> RcpApprox()"); |
| 4014 | return { 0.0f }; |
| 4015 | } |
| 4016 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 4017 | bool HasRcpSqrtApprox() |
| 4018 | { |
| 4019 | return false; |
| 4020 | } |
| 4021 | |
| 4022 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 4023 | { |
| 4024 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4025 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 4026 | return { 0.0f }; |
| 4027 | } |
| 4028 | |
| 4029 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 4030 | { |
| 4031 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4032 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 4033 | return { 0.0f }; |
| 4034 | } |
| 4035 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4036 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4037 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4038 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4039 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4040 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4041 | Float4 result; |
| 4042 | result.x = Sqrt(Float(Float4(x).x)); |
| 4043 | result.y = Sqrt(Float(Float4(x).y)); |
| 4044 | result.z = Sqrt(Float(Float4(x).z)); |
| 4045 | result.w = Sqrt(Float(Float4(x).w)); |
| 4046 | |
| 4047 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4048 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4049 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4050 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4051 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4052 | 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] | 4053 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4054 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4055 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4056 | ::basicBlock->appendInst(sqrt); |
| 4057 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4058 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4059 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4060 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4061 | |
| 4062 | RValue<Int> SignMask(RValue<Float4> x) |
| 4063 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4064 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4065 | if(emulateIntrinsics || CPUID::ARM) |
| 4066 | { |
| 4067 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4068 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4069 | } |
| 4070 | else |
| 4071 | { |
| 4072 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4073 | 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] | 4074 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4075 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4076 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4077 | ::basicBlock->appendInst(movmsk); |
| 4078 | |
| 4079 | return RValue<Int>(V(result)); |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 4084 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4085 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4086 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 4090 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4091 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4092 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 4096 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4097 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4098 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4102 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4103 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4104 | return RValue<Int4>(Nucleus::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 4108 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4109 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4110 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 4114 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4115 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4116 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 4120 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4121 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4122 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 4126 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4127 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4128 | return RValue<Int4>(Nucleus::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 4132 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4133 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4134 | return RValue<Int4>(Nucleus::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4138 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4139 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4140 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4144 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4145 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4146 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4150 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4151 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4152 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | RValue<Float4> Round(RValue<Float4> x) |
| 4156 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4157 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4158 | if(emulateIntrinsics || CPUID::ARM) |
| 4159 | { |
| 4160 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4161 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4162 | } |
| 4163 | else if(CPUID::SSE4_1) |
| 4164 | { |
| 4165 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4166 | 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] | 4167 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4168 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4169 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4170 | round->addArg(::context->getConstantInt32(0)); |
| 4171 | ::basicBlock->appendInst(round); |
| 4172 | |
| 4173 | return RValue<Float4>(V(result)); |
| 4174 | } |
| 4175 | else |
| 4176 | { |
| 4177 | return Float4(RoundInt(x)); |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | RValue<Float4> Trunc(RValue<Float4> x) |
| 4182 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4183 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4184 | if(CPUID::SSE4_1) |
| 4185 | { |
| 4186 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4187 | 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] | 4188 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4189 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4190 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4191 | round->addArg(::context->getConstantInt32(3)); |
| 4192 | ::basicBlock->appendInst(round); |
| 4193 | |
| 4194 | return RValue<Float4>(V(result)); |
| 4195 | } |
| 4196 | else |
| 4197 | { |
| 4198 | return Float4(Int4(x)); |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | RValue<Float4> Frac(RValue<Float4> x) |
| 4203 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4204 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4205 | Float4 frc; |
| 4206 | |
| 4207 | if(CPUID::SSE4_1) |
| 4208 | { |
| 4209 | frc = x - Floor(x); |
| 4210 | } |
| 4211 | else |
| 4212 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4213 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4214 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4215 | 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] | 4216 | } |
| 4217 | |
| 4218 | // x - floor(x) can be 1.0 for very small negative x. |
| 4219 | // Clamp against the value just below 1.0. |
| 4220 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4221 | } |
| 4222 | |
| 4223 | RValue<Float4> Floor(RValue<Float4> x) |
| 4224 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4225 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4226 | if(CPUID::SSE4_1) |
| 4227 | { |
| 4228 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4229 | 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] | 4230 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4231 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4232 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4233 | round->addArg(::context->getConstantInt32(1)); |
| 4234 | ::basicBlock->appendInst(round); |
| 4235 | |
| 4236 | return RValue<Float4>(V(result)); |
| 4237 | } |
| 4238 | else |
| 4239 | { |
| 4240 | return x - Frac(x); |
| 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4245 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4246 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4247 | if(CPUID::SSE4_1) |
| 4248 | { |
| 4249 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4250 | 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] | 4251 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4252 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4253 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4254 | round->addArg(::context->getConstantInt32(2)); |
| 4255 | ::basicBlock->appendInst(round); |
| 4256 | |
| 4257 | return RValue<Float4>(V(result)); |
| 4258 | } |
| 4259 | else |
| 4260 | { |
| 4261 | return -Floor(-x); |
| 4262 | } |
| 4263 | } |
| 4264 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4265 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4266 | { |
| 4267 | return T(Ice::IceType_v4f32); |
| 4268 | } |
| 4269 | |
| 4270 | RValue<Long> Ticks() |
| 4271 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4272 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4273 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4274 | return Long(Int(0)); |
| 4275 | } |
| 4276 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4277 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4278 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4279 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4280 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4281 | } |
| 4282 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4283 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4284 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4285 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4286 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4287 | } |
| 4288 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4289 | 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] | 4290 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4291 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4292 | 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] | 4293 | } |
| 4294 | |
| 4295 | void Breakpoint() |
| 4296 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4297 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4298 | 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] | 4299 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4300 | auto trap = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4301 | ::basicBlock->appendInst(trap); |
| 4302 | } |
| 4303 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4304 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4305 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4306 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4307 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicFence, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4308 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4309 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4310 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4311 | inst->addArg(order); |
| 4312 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4313 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4314 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4315 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4316 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4317 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4318 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4319 | return nullptr; |
| 4320 | } |
| 4321 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4322 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4323 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4324 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4325 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4326 | |
| 4327 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4328 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4329 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4330 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4331 | } |
| 4332 | |
| 4333 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4334 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4335 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4336 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4337 | } |
| 4338 | |
| 4339 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4340 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4341 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4342 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4343 | } |
| 4344 | |
| 4345 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4346 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4347 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4348 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4349 | } |
| 4350 | |
| 4351 | RValue<Float> Exp2(RValue<Float> x) |
| 4352 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4353 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4354 | return emulated::Exp2(x); |
| 4355 | } |
| 4356 | |
| 4357 | RValue<Float> Log2(RValue<Float> x) |
| 4358 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4359 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4360 | return emulated::Log2(x); |
| 4361 | } |
| 4362 | |
| 4363 | RValue<Float4> Sin(RValue<Float4> x) |
| 4364 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4365 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4366 | return optimal::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | RValue<Float4> Cos(RValue<Float4> x) |
| 4370 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4371 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4372 | return optimal::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | RValue<Float4> Tan(RValue<Float4> x) |
| 4376 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4377 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4378 | return optimal::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4379 | } |
| 4380 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4381 | RValue<Float4> Asin(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4382 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4383 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4384 | if(p == Precision::Full) |
| 4385 | { |
| 4386 | return emulated::Asin(x); |
| 4387 | } |
| 4388 | return optimal::Asin_8_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4389 | } |
| 4390 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4391 | RValue<Float4> Acos(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4392 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4393 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4394 | // Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial approximation |
| 4395 | // version of acos, unlike for Asin, which requires higher precision algorithms. |
| 4396 | return optimal::Acos_4_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | RValue<Float4> Atan(RValue<Float4> x) |
| 4400 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4401 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4402 | return optimal::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4406 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4407 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4408 | return optimal::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | RValue<Float4> Cosh(RValue<Float4> x) |
| 4412 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4413 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4414 | return optimal::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | RValue<Float4> Tanh(RValue<Float4> x) |
| 4418 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4419 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4420 | return optimal::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | RValue<Float4> Asinh(RValue<Float4> x) |
| 4424 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4425 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4426 | return optimal::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | RValue<Float4> Acosh(RValue<Float4> x) |
| 4430 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4431 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4432 | return optimal::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4436 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4437 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4438 | return optimal::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 4442 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4443 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4444 | return optimal::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4448 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4449 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4450 | return optimal::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | RValue<Float4> Exp(RValue<Float4> x) |
| 4454 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4455 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4456 | return optimal::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | RValue<Float4> Log(RValue<Float4> x) |
| 4460 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4461 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4462 | return optimal::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4466 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4467 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4468 | return optimal::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | RValue<Float4> Log2(RValue<Float4> x) |
| 4472 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4473 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4474 | return optimal::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4478 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4479 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4480 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4481 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4482 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4483 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4484 | } |
| 4485 | else |
| 4486 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4487 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4488 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Ctlz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4489 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4490 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4491 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4492 | ::basicBlock->appendInst(ctlz); |
| 4493 | |
| 4494 | return RValue<UInt>(V(result)); |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4499 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4500 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4501 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4502 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4503 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4504 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4505 | } |
| 4506 | else |
| 4507 | { |
| 4508 | // TODO: implement vectorized version in Subzero |
| 4509 | UInt4 result; |
| 4510 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4511 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4512 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4513 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4514 | return result; |
| 4515 | } |
| 4516 | } |
| 4517 | |
| 4518 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4519 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4520 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4521 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4522 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4523 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4524 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4525 | } |
| 4526 | else |
| 4527 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4528 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4529 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Cttz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4530 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4531 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4532 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4533 | ::basicBlock->appendInst(ctlz); |
| 4534 | |
| 4535 | return RValue<UInt>(V(result)); |
| 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4540 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4541 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4542 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4543 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4544 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4545 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4546 | } |
| 4547 | else |
| 4548 | { |
| 4549 | // TODO: implement vectorized version in Subzero |
| 4550 | UInt4 result; |
| 4551 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4552 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4553 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4554 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4555 | return result; |
| 4556 | } |
| 4557 | } |
| 4558 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4559 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4560 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4561 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4562 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4563 | } |
| 4564 | |
| 4565 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4566 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4567 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4568 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4569 | } |
| 4570 | |
| 4571 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4572 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4573 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4574 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4575 | } |
| 4576 | |
| 4577 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4578 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4579 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4580 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4581 | } |
| 4582 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4583 | void EmitDebugLocation() |
| 4584 | { |
| 4585 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4586 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4587 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4588 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4589 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4590 | void FlushDebug() {} |
| 4591 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4592 | namespace { |
| 4593 | namespace coro { |
| 4594 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4595 | // Instance data per generated coroutine |
| 4596 | // This is the "handle" type used for Coroutine functions |
| 4597 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4598 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4599 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4600 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4601 | bool done = false; // the coroutine should stop at the next yield() |
| 4602 | bool terminated = false; // the coroutine has finished. |
| 4603 | bool inRoutine = false; // is the coroutine currently executing? |
| 4604 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4605 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4606 | void *promisePtr = nullptr; |
| 4607 | }; |
| 4608 | |
| 4609 | CoroutineData *createCoroutineData() |
| 4610 | { |
| 4611 | return new CoroutineData{}; |
| 4612 | } |
| 4613 | |
| 4614 | void destroyCoroutineData(CoroutineData *coroData) |
| 4615 | { |
| 4616 | delete coroData; |
| 4617 | } |
| 4618 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4619 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4620 | // caller's call to await(). |
| 4621 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4622 | // is called. |
| 4623 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4624 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4625 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4626 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4627 | ASSERT(coroData->inRoutine); |
| 4628 | coroData->inRoutine = false; |
| 4629 | coroData->mainFiber->notify(); |
| 4630 | while(!coroData->inRoutine) |
| 4631 | { |
| 4632 | coroData->routineFiber->wait(); |
| 4633 | } |
| 4634 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4635 | } |
| 4636 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4637 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4638 | // or the coroutine terminates. |
| 4639 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4640 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4641 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4642 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4643 | ASSERT(!coroData->inRoutine); |
| 4644 | coroData->inRoutine = true; |
| 4645 | coroData->routineFiber->notify(); |
| 4646 | while(coroData->inRoutine) |
| 4647 | { |
| 4648 | coroData->mainFiber->wait(); |
| 4649 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4650 | } |
| 4651 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4652 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4653 | // until the coroutine ends, and deletes the coroutine data. |
| 4654 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4655 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4656 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4657 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4658 | ASSERT(!coroData->inRoutine); |
| 4659 | if(!coroData->terminated) |
| 4660 | { |
| 4661 | coroData->done = true; |
| 4662 | coroData->inRoutine = true; |
| 4663 | coroData->routineFiber->notify(); |
| 4664 | while(!coroData->terminated) |
| 4665 | { |
| 4666 | coroData->mainFiber->wait(); |
| 4667 | } |
| 4668 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4669 | if(coroData->useInternalScheduler) |
| 4670 | { |
| 4671 | ::getOrCreateScheduler().unbind(); |
| 4672 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4673 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4674 | } |
| 4675 | |
| 4676 | namespace detail { |
| 4677 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4678 | } // namespace detail |
| 4679 | |
| 4680 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4681 | { |
| 4682 | ASSERT(!detail::coroHandle); |
| 4683 | detail::coroHandle = handle; |
| 4684 | } |
| 4685 | |
| 4686 | Nucleus::CoroutineHandle getHandleParam() |
| 4687 | { |
| 4688 | ASSERT(detail::coroHandle); |
| 4689 | auto handle = detail::coroHandle; |
| 4690 | detail::coroHandle = {}; |
| 4691 | return handle; |
| 4692 | } |
| 4693 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4694 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4695 | { |
| 4696 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4697 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4701 | { |
| 4702 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4703 | coroData->promisePtr = promisePtr; |
| 4704 | } |
| 4705 | |
| 4706 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4707 | { |
| 4708 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4709 | return coroData->promisePtr; |
| 4710 | } |
| 4711 | |
| 4712 | } // namespace coro |
| 4713 | } // namespace |
| 4714 | |
| 4715 | // Used to generate coroutines. |
| 4716 | // Lifetime: from yield to acquireCoroutine |
| 4717 | class CoroutineGenerator |
| 4718 | { |
| 4719 | public: |
| 4720 | CoroutineGenerator() |
| 4721 | { |
| 4722 | } |
| 4723 | |
| 4724 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4725 | void generateCoroutineBegin() |
| 4726 | { |
| 4727 | // Begin building the main coroutine_begin() function. |
| 4728 | // We insert these instructions at the top of the entry node, |
| 4729 | // before existing reactor-generated instructions. |
| 4730 | |
| 4731 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4732 | // { |
| 4733 | // this->handle = coro::getHandleParam(); |
| 4734 | // |
| 4735 | // YieldType promise; |
| 4736 | // coro::setPromisePtr(handle, &promise); // For await |
| 4737 | // |
| 4738 | // ... <REACTOR CODE> ... |
| 4739 | // |
| 4740 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4741 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4742 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4743 | |
| 4744 | // YieldType promise; |
| 4745 | // coro::setPromisePtr(handle, &promise); // For await |
| 4746 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4747 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4751 | void generateYield(Value *val) |
| 4752 | { |
| 4753 | // ... <REACTOR CODE> ... |
| 4754 | // |
| 4755 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4756 | // if (!coro::suspend(handle)) { |
| 4757 | // return false; // coroutine has been stopped by the caller. |
| 4758 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4759 | // |
| 4760 | // ... <REACTOR CODE> ... |
| 4761 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4762 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4763 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4764 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4765 | // if (!coro::suspend(handle)) { |
| 4766 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4767 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4768 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4769 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4770 | |
| 4771 | // return false; // coroutine has been stopped by the caller. |
| 4772 | ::basicBlock = doneBlock; |
| 4773 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4774 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4775 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4776 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4780 | |
| 4781 | // Generates the await function for the current coroutine. |
| 4782 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4783 | static FunctionUniquePtr generateAwaitFunction() |
| 4784 | { |
| 4785 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4786 | // { |
| 4787 | // if (coro::isDone()) |
| 4788 | // { |
| 4789 | // return false; |
| 4790 | // } |
| 4791 | // else // resume |
| 4792 | // { |
| 4793 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4794 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4795 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4796 | // return true; |
| 4797 | // } |
| 4798 | // } |
| 4799 | |
| 4800 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4801 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4802 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4803 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4804 | |
| 4805 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4806 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4807 | |
| 4808 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4809 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4810 | |
| 4811 | auto doneBlock = awaitFunc->makeNode(); |
| 4812 | { |
| 4813 | // return false; |
| 4814 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4815 | doneBlock->appendInst(ret); |
| 4816 | } |
| 4817 | |
| 4818 | auto resumeBlock = awaitFunc->makeNode(); |
| 4819 | { |
| 4820 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4821 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4822 | |
| 4823 | // *out = *promise; |
| 4824 | // Load promise value |
| 4825 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4826 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4827 | resumeBlock->appendInst(load); |
| 4828 | // Then store it in output param |
| 4829 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4830 | resumeBlock->appendInst(store); |
| 4831 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4832 | // coro::resume(handle); |
| 4833 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4834 | |
| 4835 | // return true; |
| 4836 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4837 | resumeBlock->appendInst(ret); |
| 4838 | } |
| 4839 | |
| 4840 | // if (coro::isDone()) |
| 4841 | // { |
| 4842 | // <doneBlock> |
| 4843 | // } |
| 4844 | // else // resume |
| 4845 | // { |
| 4846 | // <resumeBlock> |
| 4847 | // } |
| 4848 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4849 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4850 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4851 | bb->appendInst(br); |
| 4852 | |
| 4853 | return FunctionUniquePtr{ awaitFunc }; |
| 4854 | } |
| 4855 | |
| 4856 | // Generates the destroy function for the current coroutine. |
| 4857 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4858 | static FunctionUniquePtr generateDestroyFunction() |
| 4859 | { |
| 4860 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4861 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4862 | // 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] | 4863 | // return; |
| 4864 | // } |
| 4865 | |
| 4866 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4867 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4868 | |
| 4869 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4870 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4871 | |
| 4872 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4873 | |
| 4874 | auto *bb = destroyFunc->getEntryNode(); |
| 4875 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4876 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4877 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4878 | |
| 4879 | // return; |
| 4880 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4881 | bb->appendInst(ret); |
| 4882 | |
| 4883 | return FunctionUniquePtr{ destroyFunc }; |
| 4884 | } |
| 4885 | |
| 4886 | private: |
| 4887 | Ice::Variable *handle{}; |
| 4888 | Ice::Variable *promise{}; |
| 4889 | }; |
| 4890 | |
| 4891 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4892 | { |
| 4893 | // This doubles up as our coroutine handle |
| 4894 | auto coroData = coro::createCoroutineData(); |
| 4895 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4896 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4897 | if(coroData->useInternalScheduler) |
| 4898 | { |
| 4899 | ::getOrCreateScheduler().bind(); |
| 4900 | } |
| 4901 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4902 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4903 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4904 | // any fiber switch occurs. |
| 4905 | coro::setHandleParam(coroData); |
| 4906 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4907 | ASSERT(!coroData->routineFiber); |
| 4908 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4909 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4910 | beginFunc(); |
| 4911 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4912 | ASSERT(coroData->inRoutine); |
| 4913 | coroData->done = true; // coroutine is done. |
| 4914 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4915 | coroData->inRoutine = false; |
| 4916 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4917 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4918 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4919 | ASSERT(!coroData->mainFiber); |
| 4920 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4921 | |
| 4922 | // block until the first yield or coroutine end |
| 4923 | ASSERT(!coroData->inRoutine); |
| 4924 | coroData->inRoutine = true; |
| 4925 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4926 | while(coroData->inRoutine) |
| 4927 | { |
| 4928 | coroData->mainFiber->wait(); |
| 4929 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4930 | |
| 4931 | return coroData; |
| 4932 | } |
| 4933 | |
| 4934 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4935 | { |
| 4936 | // Start by creating a regular function |
| 4937 | createFunction(yieldType, params); |
| 4938 | |
| 4939 | // Save in case yield() is called |
| 4940 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4941 | ::coroYieldType = yieldType; |
| 4942 | } |
| 4943 | |
| 4944 | void Nucleus::yield(Value *val) |
| 4945 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4946 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4947 | Variable::materializeAll(); |
| 4948 | |
| 4949 | // On first yield, we start generating coroutine functions |
| 4950 | if(!::coroGen) |
| 4951 | { |
| 4952 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4953 | ::coroGen->generateCoroutineBegin(); |
| 4954 | } |
| 4955 | |
| 4956 | ASSERT(::coroGen); |
| 4957 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4958 | } |
| 4959 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4960 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4961 | { |
| 4962 | return false; |
| 4963 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4964 | |
| 4965 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4966 | { |
| 4967 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4968 | |
| 4969 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 4970 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4971 | if(::coroGen) |
| 4972 | { |
| 4973 | // Finish generating coroutine functions |
| 4974 | { |
| 4975 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4976 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4977 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4978 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4979 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4980 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4981 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4982 | // At this point, we no longer need the CoroutineGenerator. |
| 4983 | ::coroGen.reset(); |
| 4984 | ::coroYieldType = nullptr; |
| 4985 | |
| 4986 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4987 | { name, "await", "destroy" }, |
| 4988 | cfgEdit); |
| 4989 | |
| 4990 | return routine; |
| 4991 | } |
| 4992 | else |
| 4993 | { |
| 4994 | { |
| 4995 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4996 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4997 | } |
| 4998 | |
| 4999 | ::coroYieldType = nullptr; |
| 5000 | |
| 5001 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 5002 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 5003 | |
| 5004 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 5005 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 5006 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 5007 | return routine; |
| 5008 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5009 | } |
| 5010 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5011 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5012 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 5013 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 5014 | |
| 5015 | if(isCoroutine) |
| 5016 | { |
| 5017 | return rr::invokeCoroutineBegin(func); |
| 5018 | } |
| 5019 | else |
| 5020 | { |
| 5021 | // For regular routines, just invoke the begin func directly |
| 5022 | return func(); |
| 5023 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 5024 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 5025 | |
| 5026 | } // namespace rr |