2016-02-05 00:26:19 +01:00
|
|
|
//===-- echo.cpp - tool for testing libLLVM and llvm-c API ----------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-02-05 00:26:19 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-04-08 11:19:02 +02:00
|
|
|
// This file implements the --echo command in llvm-c-test.
|
2016-02-05 00:26:19 +01:00
|
|
|
//
|
|
|
|
// This command uses the C API to read a module and output an exact copy of it
|
|
|
|
// as output. It is used to check that the resulting module matches the input
|
|
|
|
// to validate that the C API can read and write modules properly.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c-test.h"
|
2018-09-28 17:35:18 +02:00
|
|
|
#include "llvm-c/DebugInfo.h"
|
2016-02-16 06:11:24 +01:00
|
|
|
#include "llvm-c/Target.h"
|
2016-02-05 00:26:19 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2021-06-01 22:41:08 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-02-09 23:36:41 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-04-05 22:45:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-02-05 00:26:19 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// Provide DenseMapInfo for C API opaque types.
|
|
|
|
template<typename T>
|
|
|
|
struct CAPIDenseMap {};
|
|
|
|
|
2020-01-06 11:15:44 +01:00
|
|
|
// The default DenseMapInfo require to know about pointer alignment.
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
// Because the C API uses opaque pointer types, their alignment is unknown.
|
2016-02-05 00:26:19 +01:00
|
|
|
// As a result, we need to roll out our own implementation.
|
|
|
|
template<typename T>
|
|
|
|
struct CAPIDenseMap<T*> {
|
|
|
|
struct CAPIDenseMapInfo {
|
|
|
|
static inline T* getEmptyKey() {
|
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-1);
|
|
|
|
return reinterpret_cast<T*>(Val);
|
|
|
|
}
|
|
|
|
static inline T* getTombstoneKey() {
|
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-2);
|
|
|
|
return reinterpret_cast<T*>(Val);
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const T *PtrVal) {
|
|
|
|
return hash_value(PtrVal);
|
|
|
|
}
|
|
|
|
static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DenseMap<T*, T*, CAPIDenseMapInfo> Map;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef CAPIDenseMap<LLVMValueRef>::Map ValueMap;
|
2016-02-09 23:36:41 +01:00
|
|
|
typedef CAPIDenseMap<LLVMBasicBlockRef>::Map BasicBlockMap;
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-10 01:38:50 +01:00
|
|
|
struct TypeCloner {
|
|
|
|
LLVMModuleRef M;
|
|
|
|
LLVMContextRef Ctx;
|
|
|
|
|
|
|
|
TypeCloner(LLVMModuleRef M): M(M), Ctx(LLVMGetModuleContext(M)) {}
|
|
|
|
|
2016-02-14 10:30:42 +01:00
|
|
|
LLVMTypeRef Clone(LLVMValueRef Src) {
|
|
|
|
return Clone(LLVMTypeOf(Src));
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:38:50 +01:00
|
|
|
LLVMTypeRef Clone(LLVMTypeRef Src) {
|
|
|
|
LLVMTypeKind Kind = LLVMGetTypeKind(Src);
|
|
|
|
switch (Kind) {
|
|
|
|
case LLVMVoidTypeKind:
|
|
|
|
return LLVMVoidTypeInContext(Ctx);
|
|
|
|
case LLVMHalfTypeKind:
|
|
|
|
return LLVMHalfTypeInContext(Ctx);
|
[IR][BFloat] Add BFloat IR type
Summary:
The BFloat IR type is introduced to provide support for, initially, the BFloat16
datatype introduced with the Armv8.6 architecture (optional from Armv8.2
onwards). It has an 8-bit exponent and a 7-bit mantissa and behaves like an IEEE
754 floating point IR type.
This is part of a patch series upstreaming Armv8.6 features. Subsequent patches
will upstream intrinsics support and C-lang support for BFloat.
Reviewers: SjoerdMeijer, rjmccall, rsmith, liutianle, RKSimon, craig.topper, jfb, LukeGeeson, sdesmalen, deadalnix, ctetreau
Subscribers: hiraditya, llvm-commits, danielkiss, arphaman, kristof.beyls, dexonsmith
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78190
2020-04-01 00:49:38 +02:00
|
|
|
case LLVMBFloatTypeKind:
|
|
|
|
return LLVMHalfTypeInContext(Ctx);
|
2016-02-10 01:38:50 +01:00
|
|
|
case LLVMFloatTypeKind:
|
|
|
|
return LLVMFloatTypeInContext(Ctx);
|
|
|
|
case LLVMDoubleTypeKind:
|
|
|
|
return LLVMDoubleTypeInContext(Ctx);
|
|
|
|
case LLVMX86_FP80TypeKind:
|
|
|
|
return LLVMX86FP80TypeInContext(Ctx);
|
|
|
|
case LLVMFP128TypeKind:
|
|
|
|
return LLVMFP128TypeInContext(Ctx);
|
|
|
|
case LLVMPPC_FP128TypeKind:
|
|
|
|
return LLVMPPCFP128TypeInContext(Ctx);
|
|
|
|
case LLVMLabelTypeKind:
|
|
|
|
return LLVMLabelTypeInContext(Ctx);
|
|
|
|
case LLVMIntegerTypeKind:
|
|
|
|
return LLVMIntTypeInContext(Ctx, LLVMGetIntTypeWidth(Src));
|
|
|
|
case LLVMFunctionTypeKind: {
|
|
|
|
unsigned ParamCount = LLVMCountParamTypes(Src);
|
|
|
|
LLVMTypeRef* Params = nullptr;
|
|
|
|
if (ParamCount > 0) {
|
Report fatal error in the case of out of memory
This is the second part of recommit of r325224. The previous part was
committed in r325426, which deals with C++ memory allocation. Solution
for C memory allocation involved functions `llvm::malloc` and similar.
This was a fragile solution because it caused ambiguity errors in some
cases. In this commit the new functions have names like `llvm::safe_malloc`.
The relevant part of original comment is below, updated for new function
names.
Analysis of fails in the case of out of memory errors can be tricky on
Windows. Such error emerges at the point where memory allocation function
fails, but manifests itself when null pointer is used. These two points
may be distant from each other. Besides, next runs may not exhibit
allocation error.
In some cases memory is allocated by a call to some of C allocation
functions, malloc, calloc and realloc. They are used for interoperability
with C code, when allocated object has variable size and when it is
necessary to avoid call of constructors. In many calls the result is not
checked for null pointer. To simplify checks, new functions are defined
in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`.
They behave as corresponding standard functions but produce fatal error if
allocation fails. This change replaces the standard functions like 'malloc'
in the cases when the result of the allocation function is not checked
for null pointer.
Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statement is added.
Differential Revision: https://reviews.llvm.org/D43010
llvm-svn: 325551
2018-02-20 06:41:26 +01:00
|
|
|
Params = static_cast<LLVMTypeRef*>(
|
|
|
|
safe_malloc(ParamCount * sizeof(LLVMTypeRef)));
|
2016-02-10 01:38:50 +01:00
|
|
|
LLVMGetParamTypes(Src, Params);
|
|
|
|
for (unsigned i = 0; i < ParamCount; i++)
|
|
|
|
Params[i] = Clone(Params[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef FunTy = LLVMFunctionType(Clone(LLVMGetReturnType(Src)),
|
|
|
|
Params, ParamCount,
|
|
|
|
LLVMIsFunctionVarArg(Src));
|
|
|
|
if (ParamCount > 0)
|
|
|
|
free(Params);
|
|
|
|
return FunTy;
|
|
|
|
}
|
|
|
|
case LLVMStructTypeKind: {
|
|
|
|
LLVMTypeRef S = nullptr;
|
|
|
|
const char *Name = LLVMGetStructName(Src);
|
|
|
|
if (Name) {
|
2020-11-30 20:34:12 +01:00
|
|
|
S = LLVMGetTypeByName2(Ctx, Name);
|
2016-02-10 01:38:50 +01:00
|
|
|
if (S)
|
|
|
|
return S;
|
|
|
|
S = LLVMStructCreateNamed(Ctx, Name);
|
|
|
|
if (LLVMIsOpaqueStruct(Src))
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned EltCount = LLVMCountStructElementTypes(Src);
|
|
|
|
SmallVector<LLVMTypeRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(Clone(LLVMStructGetTypeAtIndex(Src, i)));
|
|
|
|
if (Name)
|
|
|
|
LLVMStructSetBody(S, Elts.data(), EltCount, LLVMIsPackedStruct(Src));
|
|
|
|
else
|
|
|
|
S = LLVMStructTypeInContext(Ctx, Elts.data(), EltCount,
|
|
|
|
LLVMIsPackedStruct(Src));
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
case LLVMArrayTypeKind:
|
|
|
|
return LLVMArrayType(
|
|
|
|
Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetArrayLength(Src)
|
|
|
|
);
|
|
|
|
case LLVMPointerTypeKind:
|
|
|
|
return LLVMPointerType(
|
|
|
|
Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetPointerAddressSpace(Src)
|
|
|
|
);
|
2020-05-15 19:45:42 +02:00
|
|
|
case LLVMVectorTypeKind:
|
2016-02-10 01:38:50 +01:00
|
|
|
return LLVMVectorType(
|
|
|
|
Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetVectorSize(Src)
|
|
|
|
);
|
2020-10-28 21:48:22 +01:00
|
|
|
case LLVMScalableVectorTypeKind:
|
|
|
|
return LLVMScalableVectorType(Clone(LLVMGetElementType(Src)),
|
|
|
|
LLVMGetVectorSize(Src));
|
2016-02-10 01:38:50 +01:00
|
|
|
case LLVMMetadataTypeKind:
|
2017-10-27 13:51:40 +02:00
|
|
|
return LLVMMetadataTypeInContext(Ctx);
|
2020-12-30 15:56:29 +01:00
|
|
|
case LLVMX86_AMXTypeKind:
|
|
|
|
return LLVMX86AMXTypeInContext(Ctx);
|
2016-02-10 01:38:50 +01:00
|
|
|
case LLVMX86_MMXTypeKind:
|
|
|
|
return LLVMX86MMXTypeInContext(Ctx);
|
2018-03-30 19:49:53 +02:00
|
|
|
case LLVMTokenTypeKind:
|
|
|
|
return LLVMTokenTypeInContext(Ctx);
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 01:38:50 +01:00
|
|
|
fprintf(stderr, "%d is not a supported typekind\n", Kind);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
};
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-04-05 22:45:04 +02:00
|
|
|
static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
|
2016-02-14 10:14:30 +01:00
|
|
|
unsigned Count = LLVMCountParams(Src);
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Count != LLVMCountParams(Dst))
|
|
|
|
report_fatal_error("Parameter count mismatch");
|
2016-02-14 10:14:30 +01:00
|
|
|
|
|
|
|
ValueMap VMap;
|
|
|
|
if (Count == 0)
|
|
|
|
return VMap;
|
|
|
|
|
|
|
|
LLVMValueRef SrcFirst = LLVMGetFirstParam(Src);
|
|
|
|
LLVMValueRef DstFirst = LLVMGetFirstParam(Dst);
|
|
|
|
LLVMValueRef SrcLast = LLVMGetLastParam(Src);
|
|
|
|
LLVMValueRef DstLast = LLVMGetLastParam(Dst);
|
|
|
|
|
|
|
|
LLVMValueRef SrcCur = SrcFirst;
|
|
|
|
LLVMValueRef DstCur = DstFirst;
|
|
|
|
LLVMValueRef SrcNext = nullptr;
|
|
|
|
LLVMValueRef DstNext = nullptr;
|
|
|
|
while (true) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
|
|
|
|
LLVMSetValueName2(DstCur, Name, NameLen);
|
2016-02-14 10:14:30 +01:00
|
|
|
|
|
|
|
VMap[SrcCur] = DstCur;
|
|
|
|
|
|
|
|
Count--;
|
|
|
|
SrcNext = LLVMGetNextParam(SrcCur);
|
|
|
|
DstNext = LLVMGetNextParam(DstCur);
|
|
|
|
if (SrcNext == nullptr && DstNext == nullptr) {
|
2016-02-14 11:06:34 +01:00
|
|
|
if (SrcCur != SrcLast)
|
|
|
|
report_fatal_error("SrcLast param does not match End");
|
|
|
|
if (DstCur != DstLast)
|
|
|
|
report_fatal_error("DstLast param does not match End");
|
2016-02-14 10:14:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-02-14 11:06:34 +01:00
|
|
|
if (SrcNext == nullptr)
|
|
|
|
report_fatal_error("SrcNext was unexpectedly null");
|
|
|
|
if (DstNext == nullptr)
|
|
|
|
report_fatal_error("DstNext was unexpectedly null");
|
2016-02-14 10:14:30 +01:00
|
|
|
|
|
|
|
LLVMValueRef SrcPrev = LLVMGetPreviousParam(SrcNext);
|
2016-02-14 11:06:34 +01:00
|
|
|
if (SrcPrev != SrcCur)
|
|
|
|
report_fatal_error("SrcNext.Previous param is not Current");
|
2016-02-14 10:14:30 +01:00
|
|
|
|
|
|
|
LLVMValueRef DstPrev = LLVMGetPreviousParam(DstNext);
|
2016-02-14 11:06:34 +01:00
|
|
|
if (DstPrev != DstCur)
|
|
|
|
report_fatal_error("DstNext.Previous param is not Current");
|
2016-02-14 10:14:30 +01:00
|
|
|
|
|
|
|
SrcCur = SrcNext;
|
|
|
|
DstCur = DstNext;
|
|
|
|
}
|
|
|
|
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Count != 0)
|
|
|
|
report_fatal_error("Parameter count does not match iteration");
|
2016-02-14 10:14:30 +01:00
|
|
|
|
|
|
|
return VMap;
|
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-04-07 07:56:20 +02:00
|
|
|
static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
|
|
|
|
if (LLVMGetValueKind(V) != K)
|
|
|
|
report_fatal_error("LLVMGetValueKind returned incorrect type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
|
|
|
|
|
|
|
|
static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
|
|
|
|
LLVMValueRef Ret = clone_constant_impl(Cst, M);
|
|
|
|
check_value_kind(Ret, LLVMGetValueKind(Cst));
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
|
2016-02-14 10:30:42 +01:00
|
|
|
if (!LLVMIsAConstant(Cst))
|
|
|
|
report_fatal_error("Expected a constant");
|
|
|
|
|
|
|
|
// Maybe it is a symbol
|
|
|
|
if (LLVMIsAGlobalValue(Cst)) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cst, &NameLen);
|
2016-02-14 10:30:42 +01:00
|
|
|
|
|
|
|
// Try function
|
2016-02-17 23:13:33 +01:00
|
|
|
if (LLVMIsAFunction(Cst)) {
|
2016-04-07 07:56:20 +02:00
|
|
|
check_value_kind(Cst, LLVMFunctionValueKind);
|
2018-11-06 02:38:14 +01:00
|
|
|
|
|
|
|
LLVMValueRef Dst = nullptr;
|
|
|
|
// Try an intrinsic
|
|
|
|
unsigned ID = LLVMGetIntrinsicID(Cst);
|
|
|
|
if (ID > 0 && !LLVMIntrinsicIsOverloaded(ID)) {
|
|
|
|
Dst = LLVMGetIntrinsicDeclaration(M, ID, nullptr, 0);
|
|
|
|
} else {
|
|
|
|
// Try a normal function
|
|
|
|
Dst = LLVMGetNamedFunction(M, Name);
|
|
|
|
}
|
|
|
|
|
2016-04-07 07:56:20 +02:00
|
|
|
if (Dst)
|
|
|
|
return Dst;
|
|
|
|
report_fatal_error("Could not find function");
|
|
|
|
}
|
|
|
|
|
2016-02-14 10:30:42 +01:00
|
|
|
// Try global variable
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAGlobalVariable(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMGlobalVariableValueKind);
|
2018-05-21 01:49:08 +02:00
|
|
|
LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
|
2016-04-07 07:56:20 +02:00
|
|
|
if (Dst)
|
|
|
|
return Dst;
|
2018-05-21 01:49:08 +02:00
|
|
|
report_fatal_error("Could not find variable");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try global alias
|
|
|
|
if (LLVMIsAGlobalAlias(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMGlobalAliasValueKind);
|
|
|
|
LLVMValueRef Dst = LLVMGetNamedGlobalAlias(M, Name, NameLen);
|
|
|
|
if (Dst)
|
|
|
|
return Dst;
|
|
|
|
report_fatal_error("Could not find alias");
|
2016-02-17 23:13:33 +01:00
|
|
|
}
|
2016-04-07 07:56:20 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "Could not find @%s\n", Name);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
// Try integer literal
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAConstantInt(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantIntValueKind);
|
|
|
|
return LLVMConstInt(TypeCloner(M).Clone(Cst),
|
|
|
|
LLVMConstIntGetZExtValue(Cst), false);
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
// Try zeroinitializer
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAConstantAggregateZero(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
|
|
|
|
return LLVMConstNull(TypeCloner(M).Clone(Cst));
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
// Try constant array
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAConstantArray(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantArrayValueKind);
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMGetArrayLength(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
|
2016-04-07 07:56:20 +02:00
|
|
|
return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
|
|
|
|
}
|
|
|
|
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
// Try constant data array
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAConstantDataArray(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantDataArrayValueKind);
|
2016-03-13 01:58:25 +01:00
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMGetArrayLength(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
|
2016-04-07 07:56:20 +02:00
|
|
|
return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
// Try constant struct
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAConstantStruct(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantStructValueKind);
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMCountStructElementTypes(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
|
|
|
|
if (LLVMGetStructName(Ty))
|
2016-04-07 07:56:20 +02:00
|
|
|
return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
|
|
|
|
return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
|
|
|
|
EltCount, LLVMIsPackedStruct(Ty));
|
|
|
|
}
|
|
|
|
|
2019-11-05 05:26:51 +01:00
|
|
|
// Try ConstantPointerNull
|
|
|
|
if (LLVMIsAConstantPointerNull(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantPointerNullValueKind);
|
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
return LLVMConstNull(Ty);
|
|
|
|
}
|
|
|
|
|
2016-02-14 10:30:42 +01:00
|
|
|
// Try undef
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsUndef(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMUndefValueValueKind);
|
|
|
|
return LLVMGetUndef(TypeCloner(M).Clone(Cst));
|
|
|
|
}
|
|
|
|
|
2020-11-24 22:55:24 +01:00
|
|
|
// Try poison
|
|
|
|
if (LLVMIsPoison(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMPoisonValueValueKind);
|
|
|
|
return LLVMGetPoison(TypeCloner(M).Clone(Cst));
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:49:53 +02:00
|
|
|
// Try null
|
|
|
|
if (LLVMIsNull(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantTokenNoneValueKind);
|
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
return LLVMConstNull(Ty);
|
|
|
|
}
|
|
|
|
|
2016-02-18 00:55:59 +01:00
|
|
|
// Try float literal
|
2016-04-07 07:56:20 +02:00
|
|
|
if (LLVMIsAConstantFP(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantFPValueKind);
|
2016-02-18 00:55:59 +01:00
|
|
|
report_fatal_error("ConstantFP is not supported");
|
2016-04-07 07:56:20 +02:00
|
|
|
}
|
|
|
|
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
// Try ConstantVector
|
|
|
|
if (LLVMIsAConstantVector(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantVectorValueKind);
|
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMGetVectorSize(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
|
|
|
|
return LLVMConstVector(Elts.data(), EltCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try ConstantDataVector
|
|
|
|
if (LLVMIsAConstantDataVector(Cst)) {
|
|
|
|
check_value_kind(Cst, LLVMConstantDataVectorValueKind);
|
|
|
|
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
|
|
|
|
unsigned EltCount = LLVMGetVectorSize(Ty);
|
|
|
|
SmallVector<LLVMValueRef, 8> Elts;
|
|
|
|
for (unsigned i = 0; i < EltCount; i++)
|
|
|
|
Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
|
|
|
|
return LLVMConstVector(Elts.data(), EltCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, if it's not a constant expression, it's a kind of constant
|
|
|
|
// which is not supported
|
2016-04-07 07:56:20 +02:00
|
|
|
if (!LLVMIsAConstantExpr(Cst))
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
report_fatal_error("Unsupported constant kind");
|
2016-04-07 07:56:20 +02:00
|
|
|
|
|
|
|
// At this point, it must be a constant expression
|
|
|
|
check_value_kind(Cst, LLVMConstantExprValueKind);
|
|
|
|
|
|
|
|
LLVMOpcode Op = LLVMGetConstOpcode(Cst);
|
|
|
|
switch(Op) {
|
|
|
|
case LLVMBitCast:
|
|
|
|
return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
|
|
|
|
TypeCloner(M).Clone(Cst));
|
|
|
|
default:
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
fprintf(stderr, "%d is not a supported opcode for constant expressions\n",
|
|
|
|
Op);
|
2016-04-07 07:56:20 +02:00
|
|
|
exit(-1);
|
2016-04-07 00:21:29 +02:00
|
|
|
}
|
2016-02-14 10:30:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
struct FunCloner {
|
|
|
|
LLVMValueRef Fun;
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMModuleRef M;
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
ValueMap VMap;
|
|
|
|
BasicBlockMap BBMap;
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-10 01:38:50 +01:00
|
|
|
FunCloner(LLVMValueRef Src, LLVMValueRef Dst): Fun(Dst),
|
|
|
|
M(LLVMGetGlobalParent(Fun)), VMap(clone_params(Src, Dst)) {}
|
|
|
|
|
|
|
|
LLVMTypeRef CloneType(LLVMTypeRef Src) {
|
|
|
|
return TypeCloner(M).Clone(Src);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTypeRef CloneType(LLVMValueRef Src) {
|
2016-02-14 10:30:42 +01:00
|
|
|
return TypeCloner(M).Clone(Src);
|
2016-02-10 01:38:50 +01:00
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
// Try to clone everything in the llvm::Value hierarchy.
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef CloneValue(LLVMValueRef Src) {
|
2016-02-09 23:36:41 +01:00
|
|
|
// First, the value may be constant.
|
2016-02-14 10:30:42 +01:00
|
|
|
if (LLVMIsAConstant(Src))
|
|
|
|
return clone_constant(Src, M);
|
2016-02-10 00:41:20 +01:00
|
|
|
|
|
|
|
// Function argument should always be in the map already.
|
2016-02-11 22:37:54 +01:00
|
|
|
auto i = VMap.find(Src);
|
|
|
|
if (i != VMap.end())
|
|
|
|
return i->second;
|
2016-02-09 23:36:41 +01:00
|
|
|
|
2016-02-11 22:37:54 +01:00
|
|
|
if (!LLVMIsAInstruction(Src))
|
|
|
|
report_fatal_error("Expected an instruction");
|
2016-02-09 23:36:41 +01:00
|
|
|
|
2016-02-11 22:37:54 +01:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
auto Builder = LLVMCreateBuilderInContext(Ctx);
|
|
|
|
auto BB = DeclareBB(LLVMGetInstructionParent(Src));
|
|
|
|
LLVMPositionBuilderAtEnd(Builder, BB);
|
|
|
|
auto Dst = CloneInstruction(Src, Builder);
|
|
|
|
LLVMDisposeBuilder(Builder);
|
|
|
|
return Dst;
|
2016-02-10 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 07:14:29 +02:00
|
|
|
void CloneAttrs(LLVMValueRef Src, LLVMValueRef Dst) {
|
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
|
|
|
for (int i = LLVMAttributeReturnIndex; i <= ArgCount; i++) {
|
|
|
|
for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
|
|
|
|
if (auto SrcA = LLVMGetCallSiteEnumAttribute(Src, i, k)) {
|
|
|
|
auto Val = LLVMGetEnumAttributeValue(SrcA);
|
|
|
|
auto A = LLVMCreateEnumAttribute(Ctx, k, Val);
|
|
|
|
LLVMAddCallSiteAttribute(Dst, i, A);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
|
2016-04-07 07:56:20 +02:00
|
|
|
check_value_kind(Src, LLVMInstructionValueKind);
|
2016-02-10 00:41:20 +01:00
|
|
|
if (!LLVMIsAInstruction(Src))
|
|
|
|
report_fatal_error("Expected an instruction");
|
|
|
|
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Src, &NameLen);
|
2016-04-07 00:21:29 +02:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
// Check if this is something we already computed.
|
|
|
|
{
|
|
|
|
auto i = VMap.find(Src);
|
2016-02-11 22:37:54 +01:00
|
|
|
if (i != VMap.end()) {
|
|
|
|
// If we have a hit, it means we already generated the instruction
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
// as a dependency to something else. We need to make sure
|
2016-02-11 22:37:54 +01:00
|
|
|
// it is ordered properly.
|
|
|
|
auto I = i->second;
|
|
|
|
LLVMInstructionRemoveFromParent(I);
|
|
|
|
LLVMInsertIntoBuilderWithName(Builder, I, Name);
|
|
|
|
return I;
|
|
|
|
}
|
2016-02-05 02:27:11 +01:00
|
|
|
}
|
2016-02-09 23:36:41 +01:00
|
|
|
|
|
|
|
// We tried everything, it must be an instruction
|
|
|
|
// that hasn't been generated already.
|
|
|
|
LLVMValueRef Dst = nullptr;
|
|
|
|
|
|
|
|
LLVMOpcode Op = LLVMGetInstructionOpcode(Src);
|
|
|
|
switch(Op) {
|
|
|
|
case LLVMRet: {
|
|
|
|
int OpCount = LLVMGetNumOperands(Src);
|
|
|
|
if (OpCount == 0)
|
|
|
|
Dst = LLVMBuildRetVoid(Builder);
|
|
|
|
else
|
2016-02-10 00:41:20 +01:00
|
|
|
Dst = LLVMBuildRet(Builder, CloneValue(LLVMGetOperand(Src, 0)));
|
2016-02-09 23:36:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMBr: {
|
2016-02-10 00:15:02 +01:00
|
|
|
if (!LLVMIsConditional(Src)) {
|
|
|
|
LLVMValueRef SrcOp = LLVMGetOperand(Src, 0);
|
|
|
|
LLVMBasicBlockRef SrcBB = LLVMValueAsBasicBlock(SrcOp);
|
|
|
|
Dst = LLVMBuildBr(Builder, DeclareBB(SrcBB));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Cond = LLVMGetCondition(Src);
|
|
|
|
LLVMValueRef Else = LLVMGetOperand(Src, 1);
|
|
|
|
LLVMBasicBlockRef ElseBB = DeclareBB(LLVMValueAsBasicBlock(Else));
|
|
|
|
LLVMValueRef Then = LLVMGetOperand(Src, 2);
|
|
|
|
LLVMBasicBlockRef ThenBB = DeclareBB(LLVMValueAsBasicBlock(Then));
|
2018-02-21 20:55:11 +01:00
|
|
|
Dst = LLVMBuildCondBr(Builder, CloneValue(Cond), ThenBB, ElseBB);
|
2016-02-09 23:36:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSwitch:
|
|
|
|
case LLVMIndirectBr:
|
|
|
|
break;
|
2016-02-18 21:38:32 +01:00
|
|
|
case LLVMInvoke: {
|
|
|
|
SmallVector<LLVMValueRef, 8> Args;
|
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
|
|
|
for (int i = 0; i < ArgCount; i++)
|
|
|
|
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
|
|
|
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
|
|
|
|
LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
|
|
|
|
LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
|
|
|
|
Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
|
|
|
|
Then, Unwind, Name);
|
2016-06-15 07:14:29 +02:00
|
|
|
CloneAttrs(Src, Dst);
|
2016-02-18 21:38:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-02-09 23:36:41 +01:00
|
|
|
case LLVMUnreachable:
|
|
|
|
Dst = LLVMBuildUnreachable(Builder);
|
|
|
|
break;
|
|
|
|
case LLVMAdd: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildAdd(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSub: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildSub(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMMul: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildMul(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMUDiv: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildUDiv(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSDiv: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildSDiv(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMURem: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildURem(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMSRem: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildSRem(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMShl: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildShl(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMLShr: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildLShr(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMAShr: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildAShr(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMAnd: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildAnd(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMOr: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildOr(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMXor: {
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildXor(Builder, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMAlloca: {
|
2016-02-10 01:38:50 +01:00
|
|
|
LLVMTypeRef Ty = CloneType(LLVMGetAllocatedType(Src));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildAlloca(Builder, Ty, Name);
|
2020-04-13 01:58:30 +02:00
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
2016-02-09 23:36:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-02-17 23:51:03 +01:00
|
|
|
case LLVMLoad: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
Dst = LLVMBuildLoad(Builder, Ptr, Name);
|
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
Improve C API support for atomicrmw and cmpxchg.
atomicrmw and cmpxchg have a volatile flag, so allow them to be get and set with LLVM{Get,Set}Volatile. atomicrmw and fence have orderings, so allow them to be get and set with LLVM{Get,Set}Ordering. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
atomicrmw and cmpxchg have a volatile flag, so allow it to be set/get with LLVMGetVolatile and LLVMSetVolatile. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
Add LLVMIsA## for CatchSwitchInst, CallBrInst and FenceInst, as well as AtomicCmpXchgInst and AtomicRMWInst.
Update llvm-c-test to include atomicrmw and fence, and to copy volatile for the four applicable instructions.
Differential Revision: https://reviews.llvm.org/D67132
llvm-svn: 372938
2019-09-26 02:58:55 +02:00
|
|
|
LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
|
|
|
|
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
|
2016-02-17 23:51:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMStore: {
|
|
|
|
LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
Dst = LLVMBuildStore(Builder, Val, Ptr);
|
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
Improve C API support for atomicrmw and cmpxchg.
atomicrmw and cmpxchg have a volatile flag, so allow them to be get and set with LLVM{Get,Set}Volatile. atomicrmw and fence have orderings, so allow them to be get and set with LLVM{Get,Set}Ordering. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
atomicrmw and cmpxchg have a volatile flag, so allow it to be set/get with LLVMGetVolatile and LLVMSetVolatile. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
Add LLVMIsA## for CatchSwitchInst, CallBrInst and FenceInst, as well as AtomicCmpXchgInst and AtomicRMWInst.
Update llvm-c-test to include atomicrmw and fence, and to copy volatile for the four applicable instructions.
Differential Revision: https://reviews.llvm.org/D67132
llvm-svn: 372938
2019-09-26 02:58:55 +02:00
|
|
|
LLVMSetOrdering(Dst, LLVMGetOrdering(Src));
|
|
|
|
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
|
2016-02-17 23:51:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMGetElementPtr: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
SmallVector<LLVMValueRef, 8> Idx;
|
|
|
|
int NumIdx = LLVMGetNumIndices(Src);
|
|
|
|
for (int i = 1; i <= NumIdx; i++)
|
|
|
|
Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
|
|
|
if (LLVMIsInBounds(Src))
|
|
|
|
Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
|
|
|
|
else
|
|
|
|
Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
|
|
|
|
break;
|
|
|
|
}
|
Improve C API support for atomicrmw and cmpxchg.
atomicrmw and cmpxchg have a volatile flag, so allow them to be get and set with LLVM{Get,Set}Volatile. atomicrmw and fence have orderings, so allow them to be get and set with LLVM{Get,Set}Ordering. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
atomicrmw and cmpxchg have a volatile flag, so allow it to be set/get with LLVMGetVolatile and LLVMSetVolatile. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
Add LLVMIsA## for CatchSwitchInst, CallBrInst and FenceInst, as well as AtomicCmpXchgInst and AtomicRMWInst.
Update llvm-c-test to include atomicrmw and fence, and to copy volatile for the four applicable instructions.
Differential Revision: https://reviews.llvm.org/D67132
llvm-svn: 372938
2019-09-26 02:58:55 +02:00
|
|
|
case LLVMAtomicRMW: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
LLVMAtomicRMWBinOp BinOp = LLVMGetAtomicRMWBinOp(Src);
|
|
|
|
LLVMAtomicOrdering Ord = LLVMGetOrdering(Src);
|
|
|
|
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
|
|
|
|
Dst = LLVMBuildAtomicRMW(Builder, BinOp, Ptr, Val, Ord, SingleThread);
|
2021-02-12 23:09:18 +01:00
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
Improve C API support for atomicrmw and cmpxchg.
atomicrmw and cmpxchg have a volatile flag, so allow them to be get and set with LLVM{Get,Set}Volatile. atomicrmw and fence have orderings, so allow them to be get and set with LLVM{Get,Set}Ordering. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
atomicrmw and cmpxchg have a volatile flag, so allow it to be set/get with LLVMGetVolatile and LLVMSetVolatile. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
Add LLVMIsA## for CatchSwitchInst, CallBrInst and FenceInst, as well as AtomicCmpXchgInst and AtomicRMWInst.
Update llvm-c-test to include atomicrmw and fence, and to copy volatile for the four applicable instructions.
Differential Revision: https://reviews.llvm.org/D67132
llvm-svn: 372938
2019-09-26 02:58:55 +02:00
|
|
|
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
|
|
|
|
LLVMSetValueName2(Dst, Name, NameLen);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-19 22:28:28 +01:00
|
|
|
case LLVMAtomicCmpXchg: {
|
|
|
|
LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Cmp = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
LLVMValueRef New = CloneValue(LLVMGetOperand(Src, 2));
|
|
|
|
LLVMAtomicOrdering Succ = LLVMGetCmpXchgSuccessOrdering(Src);
|
|
|
|
LLVMAtomicOrdering Fail = LLVMGetCmpXchgFailureOrdering(Src);
|
|
|
|
LLVMBool SingleThread = LLVMIsAtomicSingleThread(Src);
|
|
|
|
|
|
|
|
Dst = LLVMBuildAtomicCmpXchg(Builder, Ptr, Cmp, New, Succ, Fail,
|
|
|
|
SingleThread);
|
2021-02-12 23:09:18 +01:00
|
|
|
LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
|
Improve C API support for atomicrmw and cmpxchg.
atomicrmw and cmpxchg have a volatile flag, so allow them to be get and set with LLVM{Get,Set}Volatile. atomicrmw and fence have orderings, so allow them to be get and set with LLVM{Get,Set}Ordering. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
atomicrmw and cmpxchg have a volatile flag, so allow it to be set/get with LLVMGetVolatile and LLVMSetVolatile. Add missing LLVMAtomicRMWBinOpFAdd and LLVMAtomicRMWBinOpFSub enum constants. AtomicCmpXchg also has a weak flag, add a getter/setter for that too. Add a getter/setter for the binary-op of an atomicrmw.
Add LLVMIsA## for CatchSwitchInst, CallBrInst and FenceInst, as well as AtomicCmpXchgInst and AtomicRMWInst.
Update llvm-c-test to include atomicrmw and fence, and to copy volatile for the four applicable instructions.
Differential Revision: https://reviews.llvm.org/D67132
llvm-svn: 372938
2019-09-26 02:58:55 +02:00
|
|
|
LLVMSetVolatile(Dst, LLVMGetVolatile(Src));
|
|
|
|
LLVMSetWeak(Dst, LLVMGetWeak(Src));
|
|
|
|
LLVMSetValueName2(Dst, Name, NameLen);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-18 00:55:59 +01:00
|
|
|
case LLVMBitCast: {
|
|
|
|
LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-10 00:15:02 +01:00
|
|
|
case LLVMICmp: {
|
|
|
|
LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
|
2016-02-10 00:41:20 +01:00
|
|
|
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
|
2016-02-10 00:15:02 +01:00
|
|
|
Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-11 22:37:54 +01:00
|
|
|
case LLVMPHI: {
|
2017-03-30 14:59:53 +02:00
|
|
|
// We need to aggressively set things here because of loops.
|
2016-02-11 22:37:54 +01:00
|
|
|
VMap[Src] = Dst = LLVMBuildPhi(Builder, CloneType(Src), Name);
|
|
|
|
|
|
|
|
SmallVector<LLVMValueRef, 8> Values;
|
|
|
|
SmallVector<LLVMBasicBlockRef, 8> Blocks;
|
|
|
|
|
|
|
|
unsigned IncomingCount = LLVMCountIncoming(Src);
|
|
|
|
for (unsigned i = 0; i < IncomingCount; ++i) {
|
|
|
|
Blocks.push_back(DeclareBB(LLVMGetIncomingBlock(Src, i)));
|
|
|
|
Values.push_back(CloneValue(LLVMGetIncomingValue(Src, i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMAddIncoming(Dst, Values.data(), Blocks.data(), IncomingCount);
|
|
|
|
return Dst;
|
|
|
|
}
|
2016-02-09 23:36:41 +01:00
|
|
|
case LLVMCall: {
|
|
|
|
SmallVector<LLVMValueRef, 8> Args;
|
2016-02-10 01:09:37 +01:00
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
2016-02-09 23:36:41 +01:00
|
|
|
for (int i = 0; i < ArgCount; i++)
|
2016-02-10 00:41:20 +01:00
|
|
|
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
2016-02-10 01:09:37 +01:00
|
|
|
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
|
2016-02-09 23:36:41 +01:00
|
|
|
Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
|
2019-08-15 05:49:51 +02:00
|
|
|
LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
|
2016-06-15 07:14:29 +02:00
|
|
|
CloneAttrs(Src, Dst);
|
2016-02-18 21:38:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMResume: {
|
|
|
|
Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMLandingPad: {
|
|
|
|
// The landing pad API is a bit screwed up for historical reasons.
|
|
|
|
Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
|
|
|
|
unsigned NumClauses = LLVMGetNumClauses(Src);
|
|
|
|
for (unsigned i = 0; i < NumClauses; ++i)
|
|
|
|
LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
|
|
|
|
LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
|
2016-02-09 23:36:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-03-30 19:49:53 +02:00
|
|
|
case LLVMCleanupRet: {
|
|
|
|
LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMBasicBlockRef Unwind = nullptr;
|
|
|
|
if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src))
|
|
|
|
Unwind = DeclareBB(UDest);
|
|
|
|
Dst = LLVMBuildCleanupRet(Builder, CatchPad, Unwind);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMCatchRet: {
|
|
|
|
LLVMValueRef CatchPad = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMBasicBlockRef SuccBB = DeclareBB(LLVMGetSuccessor(Src, 0));
|
|
|
|
Dst = LLVMBuildCatchRet(Builder, CatchPad, SuccBB);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMCatchPad: {
|
|
|
|
LLVMValueRef ParentPad = CloneValue(LLVMGetParentCatchSwitch(Src));
|
|
|
|
SmallVector<LLVMValueRef, 8> Args;
|
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
|
|
|
for (int i = 0; i < ArgCount; i++)
|
|
|
|
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
|
|
|
|
Dst = LLVMBuildCatchPad(Builder, ParentPad,
|
|
|
|
Args.data(), ArgCount, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMCleanupPad: {
|
|
|
|
LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
SmallVector<LLVMValueRef, 8> Args;
|
|
|
|
int ArgCount = LLVMGetNumArgOperands(Src);
|
|
|
|
for (int i = 0; i < ArgCount; i++)
|
|
|
|
Args.push_back(CloneValue(LLVMGetArgOperand(Src, i)));
|
|
|
|
Dst = LLVMBuildCleanupPad(Builder, ParentPad,
|
|
|
|
Args.data(), ArgCount, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMCatchSwitch: {
|
|
|
|
LLVMValueRef ParentPad = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMBasicBlockRef UnwindBB = nullptr;
|
|
|
|
if (LLVMBasicBlockRef UDest = LLVMGetUnwindDest(Src)) {
|
|
|
|
UnwindBB = DeclareBB(UDest);
|
|
|
|
}
|
|
|
|
unsigned NumHandlers = LLVMGetNumHandlers(Src);
|
|
|
|
Dst = LLVMBuildCatchSwitch(Builder, ParentPad, UnwindBB, NumHandlers, Name);
|
|
|
|
if (NumHandlers > 0) {
|
|
|
|
LLVMBasicBlockRef *Handlers = static_cast<LLVMBasicBlockRef*>(
|
|
|
|
safe_malloc(NumHandlers * sizeof(LLVMBasicBlockRef)));
|
|
|
|
LLVMGetHandlers(Src, Handlers);
|
|
|
|
for (unsigned i = 0; i < NumHandlers; i++)
|
|
|
|
LLVMAddHandler(Dst, DeclareBB(Handlers[i]));
|
|
|
|
free(Handlers);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-02-10 01:38:50 +01:00
|
|
|
case LLVMExtractValue: {
|
|
|
|
LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
if (LLVMGetNumIndices(Src) > 1)
|
|
|
|
report_fatal_error("ExtractValue: Expected only one index");
|
|
|
|
else if (LLVMGetNumIndices(Src) < 1)
|
|
|
|
report_fatal_error("ExtractValue: Expected an index");
|
2016-02-10 01:38:50 +01:00
|
|
|
auto I = LLVMGetIndices(Src)[0];
|
|
|
|
Dst = LLVMBuildExtractValue(Builder, Agg, I, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMInsertValue: {
|
|
|
|
LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
if (LLVMGetNumIndices(Src) > 1)
|
|
|
|
report_fatal_error("InsertValue: Expected only one index");
|
|
|
|
else if (LLVMGetNumIndices(Src) < 1)
|
|
|
|
report_fatal_error("InsertValue: Expected an index");
|
2016-02-10 01:38:50 +01:00
|
|
|
auto I = LLVMGetIndices(Src)[0];
|
|
|
|
Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
|
|
|
|
break;
|
|
|
|
}
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
case LLVMExtractElement: {
|
|
|
|
LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
Dst = LLVMBuildExtractElement(Builder, Agg, Index, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMInsertElement: {
|
|
|
|
LLVMValueRef Agg = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
LLVMValueRef Index = CloneValue(LLVMGetOperand(Src, 2));
|
|
|
|
Dst = LLVMBuildInsertElement(Builder, Agg, V, Index, Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LLVMShuffleVector: {
|
|
|
|
LLVMValueRef Agg0 = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
LLVMValueRef Agg1 = CloneValue(LLVMGetOperand(Src, 1));
|
|
|
|
SmallVector<LLVMValueRef, 8> MaskElts;
|
|
|
|
unsigned NumMaskElts = LLVMGetNumMaskElements(Src);
|
|
|
|
for (unsigned i = 0; i < NumMaskElts; i++) {
|
|
|
|
int Val = LLVMGetMaskValue(Src, i);
|
2020-09-27 01:32:38 +02:00
|
|
|
if (Val == LLVMGetUndefMaskElem()) {
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
MaskElts.push_back(LLVMGetUndef(LLVMInt64Type()));
|
|
|
|
} else {
|
|
|
|
MaskElts.push_back(LLVMConstInt(LLVMInt64Type(), Val, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LLVMValueRef Mask = LLVMConstVector(MaskElts.data(), NumMaskElts);
|
|
|
|
Dst = LLVMBuildShuffleVector(Builder, Agg0, Agg1, Mask, Name);
|
|
|
|
break;
|
|
|
|
}
|
[IR] Add Freeze instruction
Summary:
- Define Instruction::Freeze, let it be UnaryOperator
- Add support for freeze to LLLexer/LLParser/BitcodeReader/BitcodeWriter
The format is `%x = freeze <ty> %v`
- Add support for freeze instruction to llvm-c interface.
- Add m_Freeze in PatternMatch.
- Erase freeze when lowering IR to SelDag.
Reviewers: deadalnix, hfinkel, efriedma, lebedev.ri, nlopes, jdoerfert, regehr, filcab, delcypher, whitequark
Reviewed By: lebedev.ri, jdoerfert
Subscribers: jfb, kristof.beyls, hiraditya, lebedev.ri, steven_wu, dexonsmith, xbolva00, delcypher, spatel, regehr, trentxintong, vsk, filcab, nlopes, mehdi_amini, deadalnix, llvm-commits
Differential Revision: https://reviews.llvm.org/D29011
2019-11-05 07:53:22 +01:00
|
|
|
case LLVMFreeze: {
|
|
|
|
LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
|
|
|
|
Dst = LLVMBuildFreeze(Builder, Arg, Name);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-09 23:36:41 +01:00
|
|
|
default:
|
|
|
|
break;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
2016-02-09 23:36:41 +01:00
|
|
|
|
|
|
|
if (Dst == nullptr) {
|
|
|
|
fprintf(stderr, "%d is not a supported opcode\n", Op);
|
|
|
|
exit(-1);
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 17:35:18 +02:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
size_t NumMetadataEntries;
|
|
|
|
auto *AllMetadata =
|
|
|
|
LLVMInstructionGetAllMetadataOtherThanDebugLoc(Src,
|
|
|
|
&NumMetadataEntries);
|
|
|
|
for (unsigned i = 0; i < NumMetadataEntries; ++i) {
|
|
|
|
unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
|
|
|
|
LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
|
|
|
|
LLVMSetMetadata(Dst, Kind, LLVMMetadataAsValue(Ctx, MD));
|
|
|
|
}
|
|
|
|
LLVMDisposeValueMetadataEntries(AllMetadata);
|
|
|
|
LLVMSetInstDebugLocation(Builder, Dst);
|
|
|
|
|
2016-04-07 07:56:20 +02:00
|
|
|
check_value_kind(Dst, LLVMInstructionValueKind);
|
2016-02-09 23:36:41 +01:00
|
|
|
return VMap[Src] = Dst;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) {
|
|
|
|
// Check if this is something we already computed.
|
|
|
|
{
|
|
|
|
auto i = BBMap.find(Src);
|
|
|
|
if (i != BBMap.end()) {
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMValueRef V = LLVMBasicBlockAsValue(Src);
|
|
|
|
if (!LLVMValueIsBasicBlock(V) || LLVMValueAsBasicBlock(V) != Src)
|
2016-02-10 00:41:20 +01:00
|
|
|
report_fatal_error("Basic block is not a basic block");
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-14 11:06:34 +01:00
|
|
|
const char *Name = LLVMGetBasicBlockName(Src);
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *VName = LLVMGetValueName2(V, &NameLen);
|
2016-02-09 23:36:41 +01:00
|
|
|
if (Name != VName)
|
|
|
|
report_fatal_error("Basic block name mismatch");
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name);
|
|
|
|
return BBMap[Src] = BB;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) {
|
|
|
|
LLVMBasicBlockRef BB = DeclareBB(Src);
|
|
|
|
|
|
|
|
// Make sure ordering is correct.
|
|
|
|
LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Src);
|
|
|
|
if (Prev)
|
|
|
|
LLVMMoveBasicBlockAfter(BB, DeclareBB(Prev));
|
|
|
|
|
|
|
|
LLVMValueRef First = LLVMGetFirstInstruction(Src);
|
|
|
|
LLVMValueRef Last = LLVMGetLastInstruction(Src);
|
|
|
|
|
|
|
|
if (First == nullptr) {
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Last != nullptr)
|
|
|
|
report_fatal_error("Has no first instruction, but last one");
|
2016-02-09 23:36:41 +01:00
|
|
|
return BB;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 01:38:50 +01:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
|
|
|
|
LLVMPositionBuilderAtEnd(Builder, BB);
|
|
|
|
|
|
|
|
LLVMValueRef Cur = First;
|
|
|
|
LLVMValueRef Next = nullptr;
|
|
|
|
while(true) {
|
2016-02-10 00:41:20 +01:00
|
|
|
CloneInstruction(Cur, Builder);
|
2016-02-09 23:36:41 +01:00
|
|
|
Next = LLVMGetNextInstruction(Cur);
|
|
|
|
if (Next == nullptr) {
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Cur != Last)
|
|
|
|
report_fatal_error("Final instruction does not match Last");
|
2016-02-09 23:36:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousInstruction(Next);
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous instruction is not Current");
|
2016-02-09 23:36:41 +01:00
|
|
|
|
|
|
|
Cur = Next;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMDisposeBuilder(Builder);
|
|
|
|
return BB;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
void CloneBBs(LLVMValueRef Src) {
|
|
|
|
unsigned Count = LLVMCountBasicBlocks(Src);
|
|
|
|
if (Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LLVMBasicBlockRef First = LLVMGetFirstBasicBlock(Src);
|
|
|
|
LLVMBasicBlockRef Last = LLVMGetLastBasicBlock(Src);
|
|
|
|
|
|
|
|
LLVMBasicBlockRef Cur = First;
|
|
|
|
LLVMBasicBlockRef Next = nullptr;
|
|
|
|
while(true) {
|
|
|
|
CloneBB(Cur);
|
|
|
|
Count--;
|
|
|
|
Next = LLVMGetNextBasicBlock(Cur);
|
|
|
|
if (Next == nullptr) {
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Cur != Last)
|
|
|
|
report_fatal_error("Final basic block does not match Last");
|
2016-02-09 23:36:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
LLVMBasicBlockRef Prev = LLVMGetPreviousBasicBlock(Next);
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous basic bloc is not Current");
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-09 23:36:41 +01:00
|
|
|
Cur = Next;
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Count != 0)
|
|
|
|
report_fatal_error("Basic block count does not match iterration");
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
2016-02-09 23:36:41 +01:00
|
|
|
};
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-04-05 22:45:04 +02:00
|
|
|
static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
|
2018-05-21 01:49:08 +02:00
|
|
|
auto Ctx = LLVMGetModuleContext(M);
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
|
|
|
|
LLVMValueRef End = LLVMGetLastGlobal(Src);
|
2016-02-17 23:30:05 +01:00
|
|
|
|
|
|
|
LLVMValueRef Cur = Begin;
|
|
|
|
LLVMValueRef Next = nullptr;
|
2016-02-17 23:13:33 +01:00
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 08:48:39 +02:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2016-02-17 23:30:05 +01:00
|
|
|
goto FunDecl;
|
2016-02-17 23:13:33 +01:00
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
while (true) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
2016-02-17 23:13:33 +01:00
|
|
|
if (LLVMGetNamedGlobal(M, Name))
|
|
|
|
report_fatal_error("GlobalVariable already cloned");
|
|
|
|
LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
|
2016-02-16 08:08:49 +01:00
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
Next = LLVMGetNextGlobal(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-17 23:30:05 +01:00
|
|
|
FunDecl:
|
2016-02-17 23:13:33 +01:00
|
|
|
Begin = LLVMGetFirstFunction(Src);
|
|
|
|
End = LLVMGetLastFunction(Src);
|
2016-02-16 09:37:01 +01:00
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 08:48:39 +02:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2018-05-21 01:49:08 +02:00
|
|
|
goto AliasDecl;
|
2016-02-16 09:37:01 +01:00
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
2016-02-05 00:26:19 +01:00
|
|
|
while (true) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
2016-02-17 23:13:33 +01:00
|
|
|
if (LLVMGetNamedFunction(M, Name))
|
|
|
|
report_fatal_error("Function already cloned");
|
2016-06-12 08:17:24 +02:00
|
|
|
auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
|
|
|
|
auto F = LLVMAddFunction(M, Name, Ty);
|
|
|
|
|
|
|
|
// Copy attributes
|
|
|
|
for (int i = LLVMAttributeFunctionIndex, c = LLVMCountParams(F);
|
|
|
|
i <= c; ++i) {
|
|
|
|
for (unsigned k = 0, e = LLVMGetLastEnumAttributeKind(); k < e; ++k) {
|
|
|
|
if (auto SrcA = LLVMGetEnumAttributeAtIndex(Cur, i, k)) {
|
|
|
|
auto Val = LLVMGetEnumAttributeValue(SrcA);
|
|
|
|
auto DstA = LLVMCreateEnumAttribute(Ctx, k, Val);
|
|
|
|
LLVMAddAttributeAtIndex(F, i, DstA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 23:13:33 +01:00
|
|
|
|
2016-02-16 08:08:49 +01:00
|
|
|
Next = LLVMGetNextFunction(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("Last function does not match End");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous function is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2018-05-21 01:49:08 +02:00
|
|
|
|
|
|
|
AliasDecl:
|
|
|
|
Begin = LLVMGetFirstGlobalAlias(Src);
|
|
|
|
End = LLVMGetLastGlobalAlias(Src);
|
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2019-02-05 19:05:44 +01:00
|
|
|
goto GlobalIFuncDecl;
|
2018-05-21 01:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
|
|
|
while (true) {
|
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
|
|
|
if (LLVMGetNamedGlobalAlias(M, Name, NameLen))
|
|
|
|
report_fatal_error("Global alias already cloned");
|
|
|
|
LLVMTypeRef CurType = TypeCloner(M).Clone(Cur);
|
|
|
|
// FIXME: Allow NULL aliasee.
|
|
|
|
LLVMAddAlias(M, CurType, LLVMGetUndef(CurType), Name);
|
|
|
|
|
|
|
|
Next = LLVMGetNextGlobalAlias(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2018-08-30 19:09:43 +02:00
|
|
|
|
2019-02-05 19:05:44 +01:00
|
|
|
GlobalIFuncDecl:
|
|
|
|
Begin = LLVMGetFirstGlobalIFunc(Src);
|
|
|
|
End = LLVMGetLastGlobalIFunc(Src);
|
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
|
|
|
report_fatal_error("Range has an end but no beginning");
|
|
|
|
goto NamedMDDecl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
|
|
|
while (true) {
|
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
|
|
|
if (LLVMGetNamedGlobalIFunc(M, Name, NameLen))
|
|
|
|
report_fatal_error("Global ifunc already cloned");
|
|
|
|
LLVMTypeRef CurType = TypeCloner(M).Clone(LLVMGlobalGetValueType(Cur));
|
|
|
|
// FIXME: Allow NULL resolver.
|
|
|
|
LLVMAddGlobalIFunc(M, Name, NameLen,
|
|
|
|
CurType, /*addressSpace*/ 0, LLVMGetUndef(CurType));
|
|
|
|
|
|
|
|
Next = LLVMGetNextGlobalIFunc(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
|
|
|
|
2018-08-30 19:09:43 +02:00
|
|
|
NamedMDDecl:
|
|
|
|
LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
|
|
|
|
LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
|
|
|
|
if (!BeginMD) {
|
|
|
|
if (EndMD != nullptr)
|
|
|
|
report_fatal_error("Range has an end but no beginning");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMNamedMDNodeRef CurMD = BeginMD;
|
|
|
|
LLVMNamedMDNodeRef NextMD = nullptr;
|
|
|
|
while (true) {
|
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
|
|
|
|
if (LLVMGetNamedMetadata(M, Name, NameLen))
|
|
|
|
report_fatal_error("Named Metadata Node already cloned");
|
|
|
|
LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
|
|
|
|
|
|
|
|
NextMD = LLVMGetNextNamedMetadata(CurMD);
|
|
|
|
if (NextMD == nullptr) {
|
|
|
|
if (CurMD != EndMD)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
|
|
|
|
if (PrevMD != CurMD)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
CurMD = NextMD;
|
|
|
|
}
|
2016-02-17 23:13:33 +01:00
|
|
|
}
|
|
|
|
|
2016-04-05 22:45:04 +02:00
|
|
|
static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMValueRef Begin = LLVMGetFirstGlobal(Src);
|
|
|
|
LLVMValueRef End = LLVMGetLastGlobal(Src);
|
2016-02-17 23:30:05 +01:00
|
|
|
|
|
|
|
LLVMValueRef Cur = Begin;
|
|
|
|
LLVMValueRef Next = nullptr;
|
2016-02-17 23:13:33 +01:00
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 08:48:39 +02:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2016-02-17 23:30:05 +01:00
|
|
|
goto FunClone;
|
2016-02-17 23:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
|
|
|
|
if (!G)
|
|
|
|
report_fatal_error("GlobalVariable must have been declared already");
|
|
|
|
|
|
|
|
if (auto I = LLVMGetInitializer(Cur))
|
|
|
|
LLVMSetInitializer(G, clone_constant(I, M));
|
|
|
|
|
2018-09-28 17:35:18 +02:00
|
|
|
size_t NumMetadataEntries;
|
|
|
|
auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
|
|
|
|
for (unsigned i = 0; i < NumMetadataEntries; ++i) {
|
|
|
|
unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
|
|
|
|
LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
|
|
|
|
LLVMGlobalSetMetadata(G, Kind, MD);
|
|
|
|
}
|
|
|
|
LLVMDisposeValueMetadataEntries(AllMetadata);
|
C API: functions to get mask of a ShuffleVector
This commit fixes a regression (from LLVM 10 to LLVM 11 RC3) in the LLVM
C API.
Previously, commit 1ee6ec2bf removed the mask operand from the
ShuffleVector instruction, storing the mask data separately in the
instruction instead; this reduced the number of operands of
ShuffleVector from 3 to 2. AFAICT, this change unintentionally caused
a regression in the LLVM C API. Specifically, it is no longer possible
to get the mask of a ShuffleVector instruction through the C API. This
patch introduces new functions which together allow a C API user to get
the mask of a ShuffleVector instruction, restoring the functionality
which was previously available through LLVMGetOperand().
This patch also adds tests for this change to the llvm-c-test
executable, which involved adding support for InsertElement,
ExtractElement, and ShuffleVector itself (as well as constant vectors)
to echo.cpp. Previously, vector operations weren't tested at all in
echo.ll.
I also fixed some typos in comments and help-text nearby these changes,
which I happened to spot while developing this patch. Since the typo
fixes are technically unrelated other than being in the same files, I'm
happy to take them out if you'd rather they not be included in the patch.
Differential Revision: https://reviews.llvm.org/D88190
2020-09-25 23:34:23 +02:00
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMSetGlobalConstant(G, LLVMIsGlobalConstant(Cur));
|
|
|
|
LLVMSetThreadLocal(G, LLVMIsThreadLocal(Cur));
|
|
|
|
LLVMSetExternallyInitialized(G, LLVMIsExternallyInitialized(Cur));
|
|
|
|
LLVMSetLinkage(G, LLVMGetLinkage(Cur));
|
|
|
|
LLVMSetSection(G, LLVMGetSection(Cur));
|
|
|
|
LLVMSetVisibility(G, LLVMGetVisibility(Cur));
|
2018-03-14 07:45:51 +01:00
|
|
|
LLVMSetUnnamedAddress(G, LLVMGetUnnamedAddress(Cur));
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMSetAlignment(G, LLVMGetAlignment(Cur));
|
|
|
|
|
|
|
|
Next = LLVMGetNextGlobal(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobal(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:30:05 +01:00
|
|
|
FunClone:
|
2016-02-17 23:13:33 +01:00
|
|
|
Begin = LLVMGetFirstFunction(Src);
|
|
|
|
End = LLVMGetLastFunction(Src);
|
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
2017-07-13 08:48:39 +02:00
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2018-05-21 01:49:08 +02:00
|
|
|
goto AliasClone;
|
2016-02-17 23:13:33 +01:00
|
|
|
}
|
2016-02-16 08:08:49 +01:00
|
|
|
|
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
|
|
|
while (true) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
2016-02-17 23:13:33 +01:00
|
|
|
LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
|
|
|
|
if (!Fun)
|
|
|
|
report_fatal_error("Function must have been declared already");
|
2016-02-18 21:38:32 +01:00
|
|
|
|
|
|
|
if (LLVMHasPersonalityFn(Cur)) {
|
2018-05-19 17:08:36 +02:00
|
|
|
size_t FNameLen;
|
|
|
|
const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
|
|
|
|
&FNameLen);
|
2016-02-18 21:38:32 +01:00
|
|
|
LLVMValueRef P = LLVMGetNamedFunction(M, FName);
|
|
|
|
if (!P)
|
|
|
|
report_fatal_error("Could not find personality function");
|
|
|
|
LLVMSetPersonalityFn(Fun, P);
|
|
|
|
}
|
|
|
|
|
2018-09-28 17:35:18 +02:00
|
|
|
size_t NumMetadataEntries;
|
|
|
|
auto *AllMetadata = LLVMGlobalCopyAllMetadata(Cur, &NumMetadataEntries);
|
|
|
|
for (unsigned i = 0; i < NumMetadataEntries; ++i) {
|
|
|
|
unsigned Kind = LLVMValueMetadataEntriesGetKind(AllMetadata, i);
|
|
|
|
LLVMMetadataRef MD = LLVMValueMetadataEntriesGetMetadata(AllMetadata, i);
|
|
|
|
LLVMGlobalSetMetadata(Fun, Kind, MD);
|
|
|
|
}
|
|
|
|
LLVMDisposeValueMetadataEntries(AllMetadata);
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
FunCloner FC(Cur, Fun);
|
|
|
|
FC.CloneBBs(Cur);
|
|
|
|
|
2016-02-05 00:26:19 +01:00
|
|
|
Next = LLVMGetNextFunction(Cur);
|
|
|
|
if (Next == nullptr) {
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("Last function does not match End");
|
2016-02-05 00:26:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousFunction(Next);
|
2016-02-14 11:06:34 +01:00
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous function is not Current");
|
2016-02-05 00:26:19 +01:00
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2018-05-21 01:49:08 +02:00
|
|
|
|
|
|
|
AliasClone:
|
|
|
|
Begin = LLVMGetFirstGlobalAlias(Src);
|
|
|
|
End = LLVMGetLastGlobalAlias(Src);
|
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
|
|
|
report_fatal_error("Range has an end but no beginning");
|
2019-02-05 19:05:44 +01:00
|
|
|
goto GlobalIFuncClone;
|
2018-05-21 01:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
|
|
|
while (true) {
|
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
|
|
|
LLVMValueRef Alias = LLVMGetNamedGlobalAlias(M, Name, NameLen);
|
|
|
|
if (!Alias)
|
|
|
|
report_fatal_error("Global alias must have been declared already");
|
|
|
|
|
|
|
|
if (LLVMValueRef Aliasee = LLVMAliasGetAliasee(Cur)) {
|
|
|
|
LLVMAliasSetAliasee(Alias, clone_constant(Aliasee, M));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMSetLinkage(Alias, LLVMGetLinkage(Cur));
|
|
|
|
LLVMSetUnnamedAddress(Alias, LLVMGetUnnamedAddress(Cur));
|
|
|
|
|
|
|
|
Next = LLVMGetNextGlobalAlias(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("Last global alias does not match End");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobalAlias(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global alias is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
2018-08-30 19:09:43 +02:00
|
|
|
|
2019-02-05 19:05:44 +01:00
|
|
|
GlobalIFuncClone:
|
|
|
|
Begin = LLVMGetFirstGlobalIFunc(Src);
|
|
|
|
End = LLVMGetLastGlobalIFunc(Src);
|
|
|
|
if (!Begin) {
|
|
|
|
if (End != nullptr)
|
|
|
|
report_fatal_error("Range has an end but no beginning");
|
|
|
|
goto NamedMDClone;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cur = Begin;
|
|
|
|
Next = nullptr;
|
|
|
|
while (true) {
|
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetValueName2(Cur, &NameLen);
|
|
|
|
LLVMValueRef IFunc = LLVMGetNamedGlobalIFunc(M, Name, NameLen);
|
|
|
|
if (!IFunc)
|
|
|
|
report_fatal_error("Global ifunc must have been declared already");
|
|
|
|
|
|
|
|
if (LLVMValueRef Resolver = LLVMGetGlobalIFuncResolver(Cur)) {
|
|
|
|
LLVMSetGlobalIFuncResolver(IFunc, clone_constant(Resolver, M));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMSetLinkage(IFunc, LLVMGetLinkage(Cur));
|
|
|
|
LLVMSetUnnamedAddress(IFunc, LLVMGetUnnamedAddress(Cur));
|
|
|
|
|
|
|
|
Next = LLVMGetNextGlobalIFunc(Cur);
|
|
|
|
if (Next == nullptr) {
|
|
|
|
if (Cur != End)
|
|
|
|
report_fatal_error("Last global alias does not match End");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMValueRef Prev = LLVMGetPreviousGlobalIFunc(Next);
|
|
|
|
if (Prev != Cur)
|
|
|
|
report_fatal_error("Next.Previous global alias is not Current");
|
|
|
|
|
|
|
|
Cur = Next;
|
|
|
|
}
|
|
|
|
|
2018-08-30 19:09:43 +02:00
|
|
|
NamedMDClone:
|
|
|
|
LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
|
|
|
|
LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
|
|
|
|
if (!BeginMD) {
|
|
|
|
if (EndMD != nullptr)
|
|
|
|
report_fatal_error("Range has an end but no beginning");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMNamedMDNodeRef CurMD = BeginMD;
|
|
|
|
LLVMNamedMDNodeRef NextMD = nullptr;
|
|
|
|
while (true) {
|
|
|
|
size_t NameLen;
|
|
|
|
const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
|
|
|
|
LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
|
|
|
|
if (!NamedMD)
|
|
|
|
report_fatal_error("Named MD Node must have been declared already");
|
|
|
|
|
|
|
|
unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
|
|
|
|
LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
|
|
|
|
safe_malloc(OperandCount * sizeof(LLVMValueRef)));
|
|
|
|
LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
|
|
|
|
for (unsigned i = 0, e = OperandCount; i != e; ++i) {
|
|
|
|
LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
|
|
|
|
}
|
|
|
|
free(OperandBuf);
|
|
|
|
|
|
|
|
NextMD = LLVMGetNextNamedMetadata(CurMD);
|
|
|
|
if (NextMD == nullptr) {
|
|
|
|
if (CurMD != EndMD)
|
|
|
|
report_fatal_error("Last Named MD Node does not match End");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
|
|
|
|
if (PrevMD != CurMD)
|
|
|
|
report_fatal_error("Next.Previous Named MD Node is not Current");
|
|
|
|
|
|
|
|
CurMD = NextMD;
|
|
|
|
}
|
2016-02-05 00:26:19 +01:00
|
|
|
}
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
int llvm_echo(void) {
|
2016-02-05 00:26:19 +01:00
|
|
|
LLVMEnablePrettyStackTrace();
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
LLVMModuleRef Src = llvm_load_module(false, true);
|
2018-01-30 22:34:29 +01:00
|
|
|
size_t SourceFileLen;
|
|
|
|
const char *SourceFileName = LLVMGetSourceFileName(Src, &SourceFileLen);
|
|
|
|
size_t ModuleIdentLen;
|
|
|
|
const char *ModuleName = LLVMGetModuleIdentifier(Src, &ModuleIdentLen);
|
2016-02-05 00:26:19 +01:00
|
|
|
LLVMContextRef Ctx = LLVMContextCreate();
|
2016-04-05 15:56:59 +02:00
|
|
|
LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
|
|
|
|
|
2018-01-30 22:34:29 +01:00
|
|
|
LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
|
|
|
|
LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
|
2016-02-05 00:26:19 +01:00
|
|
|
|
2016-02-16 06:11:24 +01:00
|
|
|
LLVMSetTarget(M, LLVMGetTarget(Src));
|
|
|
|
LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
|
|
|
|
if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
|
|
|
|
report_fatal_error("Inconsistent DataLayout string representation");
|
|
|
|
|
2018-04-06 04:31:29 +02:00
|
|
|
size_t ModuleInlineAsmLen;
|
|
|
|
const char *ModuleAsm = LLVMGetModuleInlineAsm(Src, &ModuleInlineAsmLen);
|
|
|
|
LLVMSetModuleInlineAsm2(M, ModuleAsm, ModuleInlineAsmLen);
|
|
|
|
|
2016-02-17 23:13:33 +01:00
|
|
|
declare_symbols(Src, M);
|
|
|
|
clone_symbols(Src, M);
|
2016-02-14 10:30:42 +01:00
|
|
|
char *Str = LLVMPrintModuleToString(M);
|
2016-02-05 00:26:19 +01:00
|
|
|
fputs(Str, stdout);
|
|
|
|
|
|
|
|
LLVMDisposeMessage(Str);
|
2018-02-21 20:55:11 +01:00
|
|
|
LLVMDisposeModule(Src);
|
2016-02-14 10:30:42 +01:00
|
|
|
LLVMDisposeModule(M);
|
2016-02-05 00:26:19 +01:00
|
|
|
LLVMContextDispose(Ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|