diff --git a/llvm/docs/BitCodeFormat.rst b/llvm/docs/BitCodeFormat.rst --- a/llvm/docs/BitCodeFormat.rst +++ b/llvm/docs/BitCodeFormat.rst @@ -1338,6 +1338,21 @@ The ``X86_AMX`` record (code 24) adds an ``x86_amx`` type to the type table. +TYPE_CODE_TARGET_TYPE Record +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``[TARGET_TYPE, num_tys, ...ty_params..., ...int_params... ]`` + +The ``TARGET_TYPE`` record (code 26) adds a target extension type to the type +table, with a name defined by a previously encountered ``STRUCT_NAME`` record. +The operand fields are + +* *num_tys*: The number of parameters that are types (as opposed to integers) + +* *ty_params*: Type indices that represent type parameters + +* *int_params*: Numbers that correspond to the integer parameters. + .. _CONSTANTS_BLOCK: CONSTANTS_BLOCK Contents diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -3659,6 +3659,46 @@ pointers" are still supported under non-default options. See the `opaque pointers document `__ for more information. +.. _t_target_type: + +Target Extension Type +""""""""""""""""""""" + +:Overview: + +Target extension types represent types that must be preserved through +optimization, but are otherwise generally opaque to the compiler. They may be +used as function parameters or arguments, and in :ref:`phi ` or +:ref:`select ` instructions. They may be also used in +:ref:`alloca ` instructions or as global values, and correspondingly +it is legal to use :ref:`load ` and :ref:`store ` instructions +on them. Full semantics for these types are defined by the target. + +The only constants that target extension types may have are ``zeroinitializer``, +``undef``, and ``poison``. Other possible values for target extension types may +arise from target-specific intrinsics and functions. + +These types cannot be converted to other types. As such, it is not legal to use +them in :ref:`bitcast ` instructions (as a source or target type), +nor is it legal to use them in :ref:`ptrtoint ` or +:ref:`inttoptr ` instructions. Similarly, they are not legal to use +in an :ref:`icmp ` instruction. + +Target extension types have a name and optional type or integer parameters. The +meanings of name and parameters are defined by the target. When being defined in +LLVM IR, all of the type parameters must precede all of the integer parameters. + +:Syntax: + +.. code-block:: llvm + + target("label") + target("label", void) + target("label", void, i32) + target("label", 0, 1, 2) + target("label", void, i32, 0, 1, 2) + + .. _t_vector: Vector Type diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -165,7 +165,8 @@ LLVMTokenTypeKind, /**< Tokens */ LLVMScalableVectorTypeKind, /**< Scalable SIMD vector type */ LLVMBFloatTypeKind, /**< 16 bit brain floating point type */ - LLVMX86_AMXTypeKind /**< X86 AMX */ + LLVMX86_AMXTypeKind, /**< X86 AMX */ + LLVMTargetExtTypeKind, /**< Target extension type */ } LLVMTypeKind; typedef enum { @@ -284,7 +285,8 @@ LLVMInlineAsmValueKind, LLVMInstructionValueKind, - LLVMPoisonValueValueKind + LLVMPoisonValueValueKind, + LLVMConstantOpaqueNoneValueKind } LLVMValueKind; typedef enum { @@ -1561,6 +1563,15 @@ LLVMTypeRef LLVMX86MMXType(void); LLVMTypeRef LLVMX86AMXType(void); +/** + * Create a target extension type in LLVM context. + */ +LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name, + LLVMTypeRef *TypeParams, + unsigned TypeParamCount, + unsigned *IntParams, + unsigned IntParamCount); + /** * @} */ diff --git a/llvm/include/llvm-c/Types.h b/llvm/include/llvm-c/Types.h --- a/llvm/include/llvm-c/Types.h +++ b/llvm/include/llvm-c/Types.h @@ -65,7 +65,7 @@ * * @see llvm::Type */ -typedef struct LLVMOpaqueType *LLVMTypeRef; +typedef struct LLVMTargetExtType *LLVMTypeRef; /** * Represents an individual value in LLVM IR. diff --git a/llvm/include/llvm/AsmParser/LLParser.h b/llvm/include/llvm/AsmParser/LLParser.h --- a/llvm/include/llvm/AsmParser/LLParser.h +++ b/llvm/include/llvm/AsmParser/LLParser.h @@ -423,6 +423,7 @@ bool parseArrayVectorType(Type *&Result, bool IsVector); bool parseFunctionType(Type *&Result); + bool parseTargetExtType(Type *&Result); // Function Semantic Analysis. class PerFunctionState { diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h --- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h +++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h @@ -175,6 +175,8 @@ TYPE_CODE_X86_AMX = 24, // X86 AMX TYPE_CODE_OPAQUE_POINTER = 25, // OPAQUE_POINTER: [addrspace] + + TYPE_CODE_TARGET_TYPE = 26, // TARGET_TYPE }; enum OperandBundleTagCode { diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -844,6 +844,34 @@ } }; +/// A constant opaque type default initializer +/// +class ConstantOpaqueNone final : public ConstantData { + friend class Constant; + + explicit ConstantOpaqueNone(TargetExtType *T) + : ConstantData(T, Value::ConstantOpaqueNoneVal) {} + + void destroyConstantImpl(); + +public: + ConstantOpaqueNone(const ConstantOpaqueNone &) = delete; + + /// Static factory methods - Return objects of the specified value + static ConstantOpaqueNone *get(TargetExtType *T); + + /// Specialize the getType() method to always return an TargetExtType, + /// which reduces the amount of casting needed in parts of the compiler. + inline TargetExtType *getType() const { + return cast(Value::getType()); + } + + /// Methods for support type inquiry through isa, cast, and dyn_cast: + static bool classof(const Value *V) { + return V->getValueID() == ConstantOpaqueNoneVal; + } +}; + /// The address of a basic block. /// class BlockAddress final : public Constant { diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -713,6 +713,10 @@ getTypeSizeInBits(VTy->getElementType()).getFixedSize(); return TypeSize(MinBits, EltCnt.isScalable()); } + case Type::TargetExtTyID: { + Type *LayoutTy = cast(Ty)->getLayoutType(); + return getTypeSizeInBits(LayoutTy); + } default: llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type"); } diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h --- a/llvm/include/llvm/IR/DerivedTypes.h +++ b/llvm/include/llvm/IR/DerivedTypes.h @@ -730,6 +730,74 @@ return cast(getScalarType())->getAddressSpace(); } +/// Class to represent target extensions types, which are generally +/// unintrospectable from target-independent optimizations. +/// +/// Target extension types have a string name, and optionally have type and/or +/// integer parameters. The exact meaning of any parameters is dependent on the +/// target. +class TargetExtType : public Type { + TargetExtType(LLVMContext &C, StringRef Name, ArrayRef Types, + ArrayRef Ints); + + std::string Name; + unsigned *IntParams; + +public: + TargetExtType(const TargetExtType &) = delete; + TargetExtType &operator=(const TargetExtType &) = delete; + + /// Return a target extension type having the specified name and optional + /// type and integer parameters. + static TargetExtType *get(LLVMContext &Context, StringRef Name, + ArrayRef Types = None, + ArrayRef Ints = None); + + /// Return the name for this target extension type. Two distinct target + /// extension types may have the same name if their type or integer parameters + /// differ. + StringRef getName() const { return Name; } + + /// Return the type parameters for this particular target extension type. If + /// there are no parameters, an empty array is returned. + ArrayRef type_params() const { + return makeArrayRef(type_param_begin(), type_param_end()); + } + + using type_param_iterator = Type::subtype_iterator; + type_param_iterator type_param_begin() const { return ContainedTys; } + type_param_iterator type_param_end() const { + return &ContainedTys[NumContainedTys]; + } + + Type *getTypeParameter(unsigned i) const { return getContainedType(i); } + unsigned getNumTypeParameters() const { return getNumContainedTypes(); } + + /// Return the integer parameters for this particular target extension type. + /// If there are no parameters, an empty array is returned. + ArrayRef int_params() const { + return makeArrayRef(IntParams, getNumIntParameters()); + } + + unsigned getIntParameter(unsigned i) const { return IntParams[i]; } + unsigned getNumIntParameters() const { return getSubclassData(); } + + /// Methods for support type inquiry through isa, cast, and dyn_cast. + static bool classof(const Type *T) { return T->getTypeID() == TargetExtTyID; } + + enum Property { + HasZeroInit = 1U << 0, + CanBeGlobal = 1U << 1, + }; + + bool hasProperty(Property Prop) const; + Type *getLayoutType() const; +}; + +StringRef Type::getTargetExtName() const { + return cast(this)->getName(); +} + } // end namespace llvm #endif // LLVM_IR_DERIVEDTYPES_H diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h --- a/llvm/include/llvm/IR/Type.h +++ b/llvm/include/llvm/IR/Type.h @@ -76,6 +76,7 @@ FixedVectorTyID, ///< Fixed width SIMD vector type ScalableVectorTyID, ///< Scalable SIMD vector type TypedPointerTyID, ///< Typed pointer used by some GPU targets + TargetExtTyID, ///< Target extension type }; private: @@ -180,6 +181,9 @@ /// Return true if this is X86 AMX. bool isX86_AMXTy() const { return getTypeID() == X86_AMXTyID; } + /// Return true if this is a target extension type. + bool isTargetExtTy() const { return getTypeID() == TargetExtTyID; } + /// Return true if this is a FP type or a vector of FP. bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); } @@ -253,7 +257,7 @@ /// includes all first-class types except struct and array types. bool isSingleValueType() const { return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() || - isPointerTy() || isVectorTy() || isX86_AMXTy(); + isPointerTy() || isVectorTy() || isX86_AMXTy() || isTargetExtTy(); } /// Return true if the type is an aggregate type. This means it is valid as @@ -270,7 +274,7 @@ // If it's a primitive, it is always sized. if (getTypeID() == IntegerTyID || isFloatingPointTy() || getTypeID() == PointerTyID || getTypeID() == X86_MMXTyID || - getTypeID() == X86_AMXTyID) + getTypeID() == X86_AMXTyID || getTypeID() == TargetExtTyID) return true; // If it is not something that can have a size (e.g. a function or label), // it doesn't have a size. @@ -372,6 +376,8 @@ return ContainedTys[0]; } + inline StringRef getTargetExtName() const; + /// This method is deprecated without replacement. Pointer element types are /// not available with opaque pointers. [[deprecated("Deprecated without replacement, see " diff --git a/llvm/include/llvm/IR/Value.def b/llvm/include/llvm/IR/Value.def --- a/llvm/include/llvm/IR/Value.def +++ b/llvm/include/llvm/IR/Value.def @@ -95,6 +95,7 @@ HANDLE_CONSTANT(ConstantDataVector) HANDLE_CONSTANT(ConstantInt) HANDLE_CONSTANT(ConstantFP) +HANDLE_CONSTANT(ConstantOpaqueNone) HANDLE_CONSTANT(ConstantPointerNull) HANDLE_CONSTANT(ConstantTokenNone) diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -2522,6 +2522,12 @@ return false; } break; + case lltok::kw_target: { + // Type ::= TargetExtType + if (parseTargetExtType(Result)) + return true; + break; + } case lltok::lbrace: // Type ::= StructType if (parseAnonStructType(Result, false)) @@ -3029,6 +3035,60 @@ return false; } +/// parseTargetExtType - handle opaque type syntax +/// TargetExtType +/// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')' +/// +/// TargetExtTypeParams +/// ::= /*empty*/ +/// ::= ',' Type TargetExtTypeParams +/// +/// TargetExtIntParams +/// ::= /*empty*/ +/// ::= ',' uint32 TargetExtIntParams +bool LLParser::parseTargetExtType(Type *&Result) { + Lex.Lex(); // Eat the 'target' keyword. + + // Get the mandatory type name. + std::string TypeName; + if (parseToken(lltok::lparen, "expected '(' in target extension type") || + parseStringConstant(TypeName)) + return true; + + // Parse all of the integer and type parameters at the same time; the use of + // SeenInt will allow us to catch cases where type parameters follow integer + // parameters. + SmallVector TypeParams; + SmallVector IntParams; + bool SeenInt = false; + while (Lex.getKind() == lltok::comma) { + Lex.Lex(); // Eat the comma. + + if (Lex.getKind() == lltok::APSInt) { + SeenInt = true; + unsigned IntVal; + if (parseUInt32(IntVal)) + return true; + IntParams.push_back(IntVal); + } else if (SeenInt) { + // The only other kind of parameter we support is type parameters, which + // must precede the integer parameters. This is therefore an error. + return tokError("expected uint32 param"); + } else { + Type *TypeParam; + if (parseType(TypeParam, /*AllowVoid=*/true)) + return true; + TypeParams.push_back(TypeParam); + } + } + + if (parseToken(lltok::rparen, "expected ')' in target extension type")) + return true; + + Result = TargetExtType::get(Context, TypeName, TypeParams, IntParams); + return false; +} + //===----------------------------------------------------------------------===// // Function Semantic Analysis. //===----------------------------------------------------------------------===// diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -2412,6 +2412,35 @@ ResultTy = Res; break; } + case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...] + if (Record.size() < 1) + return error("Invalid target extension type record"); + + if (NumRecords >= TypeList.size()) + return error("Invalid TYPE table"); + + if (Record[0] >= Record.size()) + return error("Too many type parameters"); + + unsigned NumTys = Record[0]; + SmallVector TypeParams; + SmallVector IntParams; + for (unsigned i = 0; i < NumTys; i++) { + if (Type *T = getTypeByID(Record[i + 1])) + TypeParams.push_back(T); + else + return error("Invalid type"); + } + + for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) { + if (Record[i] > UINT_MAX) + return error("Integer parameter too large"); + IntParams.push_back(Record[i]); + } + ResultTy = TargetExtType::get(Context, TypeName, TypeParams, IntParams); + TypeName.clear(); + break; + } case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] if (Record.size() < 2) return error("Invalid array type record"); diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -1033,6 +1033,18 @@ TypeVals.push_back(true); break; } + case Type::TargetExtTyID: { + TargetExtType *TET = cast(T); + Code = bitc::TYPE_CODE_TARGET_TYPE; + writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, TET->getName(), + StructNameAbbrev); + TypeVals.push_back(TET->getNumTypeParameters()); + for (Type *InnerTy : TET->type_params()) + TypeVals.push_back(VE.getTypeID(InnerTy)); + for (unsigned IntParam : TET->int_params()) + TypeVals.push_back(IntParam); + break; + } case Type::TypedPointerTyID: llvm_unreachable("Typed pointers cannot be added to IR modules"); } diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -623,6 +623,17 @@ << TPTy->getAddressSpace() << ")"; return; } + case Type::TargetExtTyID: + TargetExtType *TETy = cast(Ty); + OS << "target(\""; + printEscapedString(Ty->getTargetExtName(), OS); + OS << "\""; + for (Type *Inner : TETy->type_params()) + OS << ", " << *Inner; + for (unsigned IntParam : TETy->int_params()) + OS << ", " << IntParam; + OS << ")"; + return; } llvm_unreachable("Invalid TypeID"); } @@ -1439,7 +1450,7 @@ return; } - if (isa(CV)) { + if (isa(CV) || isa(CV)) { Out << "zeroinitializer"; return; } diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -87,7 +87,7 @@ // constant zero is zero for aggregates, cpnull is null for pointers, none for // tokens. return isa(this) || isa(this) || - isa(this); + isa(this) || isa(this); } bool Constant::isAllOnesValue() const { @@ -369,6 +369,8 @@ return ConstantAggregateZero::get(Ty); case Type::TokenTyID: return ConstantTokenNone::get(Ty->getContext()); + case Type::TargetExtTyID: + return ConstantOpaqueNone::get(cast(Ty)); default: // Function, Label, or Opaque type? llvm_unreachable("Cannot create a null constant of that type!"); @@ -1710,6 +1712,23 @@ getContext().pImpl->CPNConstants.erase(getType()); } +//---- ConstantOpaqueNone::get() implementation. +// + +ConstantOpaqueNone *ConstantOpaqueNone::get(TargetExtType *Ty) { + std::unique_ptr &Entry = + Ty->getContext().pImpl->CONConstants[Ty]; + if (!Entry) + Entry.reset(new ConstantOpaqueNone(Ty)); + + return Entry.get(); +} + +/// Remove the constant from the constant table. +void ConstantOpaqueNone::destroyConstantImpl() { + getContext().pImpl->CONConstants.erase(getType()); +} + UndefValue *UndefValue::get(Type *Ty) { std::unique_ptr &Entry = Ty->getContext().pImpl->UVConstants[Ty]; if (!Entry) diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -529,6 +529,8 @@ return LLVMTokenTypeKind; case Type::ScalableVectorTyID: return LLVMScalableVectorTypeKind; + case Type::TargetExtTyID: + return LLVMTargetExtTypeKind; case Type::TypedPointerTyID: llvm_unreachable("Typed pointers are unsupported via the C API"); } @@ -847,6 +849,17 @@ return LLVMLabelTypeInContext(LLVMGetGlobalContext()); } +LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name, + LLVMTypeRef *TypeParams, + unsigned TypeParamCount, + unsigned *IntParams, + unsigned IntParamCount) { + ArrayRef TypeParamArray(unwrap(TypeParams), TypeParamCount); + ArrayRef IntParamArray(IntParams, IntParamCount); + return wrap( + TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray)); +} + /*===-- Operations on values ----------------------------------------------===*/ /*--.. Operations on all values ............................................--*/ diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -816,6 +816,10 @@ } case Type::X86_AMXTyID: return Align(64); + case Type::TargetExtTyID: { + Type *LayoutTy = cast(Ty)->getLayoutType(); + return getAlignment(LayoutTy, abi_or_pref); + } default: llvm_unreachable("Bad type for getAlignment!!!"); } diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -852,6 +852,15 @@ Result += "nx"; Result += "v" + utostr(EC.getKnownMinValue()) + getMangledTypeStr(VTy->getElementType(), HasUnnamedType); + } else if (TargetExtType *TETy = dyn_cast(Ty)) { + Result += "t"; + Result += TETy->getName(); + for (Type *ParamTy : TETy->type_params()) + Result += "_" + getMangledTypeStr(ParamTy, HasUnnamedType); + for (unsigned IntParam : TETy->int_params()) + Result += "_" + utostr(IntParam); + // Ensure nested target extension types are distinguishable. + Result += "t"; } else if (Ty) { switch (Ty->getTypeID()) { default: llvm_unreachable("Unhandled type"); diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -191,6 +191,55 @@ } }; +struct TargetExtTypeKeyInfo { + struct KeyTy { + StringRef Name; + ArrayRef TypeParams; + ArrayRef IntParams; + + KeyTy(StringRef N, const ArrayRef &TP, const ArrayRef &IP) + : Name(N), TypeParams(TP), IntParams(IP) {} + KeyTy(const TargetExtType *TT) + : Name(TT->getName()), TypeParams(TT->type_params()), + IntParams(TT->int_params()) {} + + bool operator==(const KeyTy &that) const { + return Name == that.Name && TypeParams == that.TypeParams && + IntParams == that.IntParams; + } + bool operator!=(const KeyTy &that) const { return !this->operator==(that); } + }; + + static inline TargetExtType *getEmptyKey() { + return DenseMapInfo::getEmptyKey(); + } + + static inline TargetExtType *getTombstoneKey() { + return DenseMapInfo::getTombstoneKey(); + } + + static unsigned getHashValue(const KeyTy &Key) { + return hash_combine( + Key.Name, + hash_combine_range(Key.TypeParams.begin(), Key.TypeParams.end()), + hash_combine_range(Key.IntParams.begin(), Key.IntParams.end())); + } + + static unsigned getHashValue(const TargetExtType *FT) { + return getHashValue(KeyTy(FT)); + } + + static bool isEqual(const KeyTy &LHS, const TargetExtType *RHS) { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + return LHS == KeyTy(RHS); + } + + static bool isEqual(const TargetExtType *LHS, const TargetExtType *RHS) { + return LHS == RHS; + } +}; + /// Structure for hashing arbitrary MDNode operands. class MDNodeOpsKey { ArrayRef RawOps; @@ -1441,6 +1490,8 @@ DenseMap> CPNConstants; + DenseMap> CONConstants; + DenseMap> UVConstants; DenseMap> PVConstants; @@ -1481,6 +1532,9 @@ StringMap NamedStructTypes; unsigned NamedStructTypesUniqueID = 0; + using TargetExtTypeSet = DenseSet; + TargetExtTypeSet TargetExtTypes; + DenseMap, ArrayType *> ArrayTypes; DenseMap, VectorType *> VectorTypes; DenseMap PointerTypes; // Pointers in AddrSpace = 0 diff --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp --- a/llvm/lib/IR/LLVMContextImpl.cpp +++ b/llvm/lib/IR/LLVMContextImpl.cpp @@ -113,6 +113,7 @@ CAZConstants.clear(); CPNConstants.clear(); + CONConstants.clear(); UVConstants.clear(); PVConstants.clear(); IntConstants.clear(); diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -784,3 +784,85 @@ bool PointerType::isLoadableOrStorableType(Type *ElemTy) { return isValidElementType(ElemTy) && !ElemTy->isFunctionTy(); } + +//===----------------------------------------------------------------------===// +// TargetExtType Implementation +//===----------------------------------------------------------------------===// + +TargetExtType::TargetExtType(LLVMContext &C, StringRef Name, + ArrayRef Types, ArrayRef Ints) + : Type(C, TargetExtTyID), Name(Name) { + NumContainedTys = Types.size(); + + // Parameter storage immediately follows the class in allocation. + Type **Params = reinterpret_cast(this + 1); + ContainedTys = Params; + for (Type *T : Types) + *Params++ = T; + + setSubclassData(Ints.size()); + unsigned *IntParamSpace = reinterpret_cast(Params); + IntParams = IntParamSpace; + for (unsigned IntParam : Ints) + *IntParamSpace++ = IntParam; +} + +TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name, + ArrayRef Types, + ArrayRef Ints) { + const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints); + TargetExtType *TT; + // Since we only want to allocate a fresh target type in case none is found + // and we don't want to perform two lookups (one for checking if existent and + // one for inserting the newly allocated one), here we instead lookup based on + // Key and update the reference to the opaque type in-place to a newly + // allocated one if not found. + auto Insertion = C.pImpl->TargetExtTypes.insert_as(nullptr, Key); + if (Insertion.second) { + // The target type was not found. Allocate one and update TargetExtTypes + // in-place. + TT = (TargetExtType *)C.pImpl->Alloc.Allocate( + sizeof(TargetExtType) + sizeof(Type *) * Types.size() + + sizeof(unsigned) * Ints.size(), + alignof(TargetExtType)); + new (TT) TargetExtType(C, Name, Types, Ints); + *Insertion.first = TT; + } else { + // The target type was found. Just return it. + TT = *Insertion.first; + } + return TT; +} + +struct TargetTypeInfo { + Type *LayoutType; + uint64_t Properties; + + template + TargetTypeInfo(Type *LayoutType, ArgTys... Properties) + : LayoutType(LayoutType), Properties((0 | ... | Properties)) { + } +}; + +static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) { + LLVMContext &C = Ty->getContext(); + StringRef Name = Ty->getName(); + if (Name.startswith("spirv.")) { + return TargetTypeInfo(Type::getInt8PtrTy(C, 0), + TargetExtType::HasZeroInit, + TargetExtType::CanBeGlobal); + } + if (Name == "aarch64.svcount") { + return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16)); + } + return TargetTypeInfo(Type::getInt1Ty(C)); +} + +Type *TargetExtType::getLayoutType() const { + return getTargetTypeInfo(this).LayoutType; +} + +bool TargetExtType::hasProperty(Property Prop) const { + uint64_t Properties = getTargetTypeInfo(this).Properties; + return (Properties & Prop) == Prop; +} diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -1719,6 +1719,9 @@ return false; } + if (OldTy->isTargetExtTy() || NewTy->isTargetExtTy()) + return false; + return true; } diff --git a/llvm/lib/Transforms/Utils/VNCoercion.cpp b/llvm/lib/Transforms/Utils/VNCoercion.cpp --- a/llvm/lib/Transforms/Utils/VNCoercion.cpp +++ b/llvm/lib/Transforms/Utils/VNCoercion.cpp @@ -57,10 +57,13 @@ // The implementation below uses inttoptr for vectors of unequal size; we // can't allow this for non integral pointers. We could teach it to extract - // exact subvectors if desired. + // exact subvectors if desired. if (StoredNI && StoreSize != DL.getTypeSizeInBits(LoadTy).getFixedSize()) return false; + if (StoredTy->isTargetExtTy() || LoadTy->isTargetExtTy()) + return false; + return true; } diff --git a/llvm/test/Assembler/invalid-opaque-type-mixed.ll b/llvm/test/Assembler/invalid-opaque-type-mixed.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Assembler/invalid-opaque-type-mixed.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s + +; CHECK: expected uint32 param +define void @f(target("type", i32, 0, void) %a) { + ret void +} diff --git a/llvm/test/Assembler/target-type-mangled.ll b/llvm/test/Assembler/target-type-mangled.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Assembler/target-type-mangled.ll @@ -0,0 +1,11 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s +; Check support for mangling of target extension types in intrinsics + +declare target("a", target("b")) @llvm.ssa.copy.ta_tbtt(target("a", target("b")) returned) +declare target("a", void, i8, 5, 3) @llvm.ssa.copy.ta_isVoid_i8_5_3t(target("a", void, i8, 5, 3) returned) +declare target("b") @llvm.ssa.copy.tbt(target("b") returned) + +; CHECK: declare target("a", target("b")) @llvm.ssa.copy.ta_tbtt(target("a", target("b")) returned) +; CHECK: declare target("a", void, i8, 5, 3) @llvm.ssa.copy.ta_isVoid_i8_5_3t(target("a", void, i8, 5, 3) returned) +; CHECK: declare target("b") @llvm.ssa.copy.tbt(target("b") returned) + diff --git a/llvm/test/Assembler/target-type-params.ll b/llvm/test/Assembler/target-type-params.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Assembler/target-type-params.ll @@ -0,0 +1,16 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s +; Check support for basic opaque type properties + +@g1 = global target("atype", void) zeroinitializer +@g2 = global target("atype", i32) zeroinitializer +@g3 = global target("atype", i32, 0) zeroinitializer +@g4 = global target("atype", 0) zeroinitializer +@g5 = global target("atype", 0, 1, 2) zeroinitializer +@g6 = global target("atype", void, i32, float, {i32, bfloat}, 0, 1, 2) zeroinitializer + +;CHECK: @g1 = global target("atype", void) zeroinitializer +;CHECK: @g2 = global target("atype", i32) zeroinitializer +;CHECK: @g3 = global target("atype", i32, 0) zeroinitializer +;CHECK: @g4 = global target("atype", 0) zeroinitializer +;CHECK: @g5 = global target("atype", 0, 1, 2) zeroinitializer +;CHECK: @g6 = global target("atype", void, i32, float, { i32, bfloat }, 0, 1, 2) zeroinitializer diff --git a/llvm/test/Assembler/target-types.ll b/llvm/test/Assembler/target-types.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Assembler/target-types.ll @@ -0,0 +1,24 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s +; Check support for basic opaque type properties + +@global = global target("ctype") zeroinitializer + +define target("atype") @foo(target("atype") %a) { + ret target("atype") %a +} + +define target("btype") @func2() { + %mem = alloca target("btype") + %val = load target("btype"), ptr %mem + ret target("btype") poison +} + +; CHECK: @global = global target("ctype") zeroinitializer +; CHECK: define target("atype") @foo(target("atype") %a) { +; CHECK: ret target("atype") %a +; CHECK: } +; CHECK: define target("btype") @func2() { +; CHECK: %mem = alloca target("btype") +; CHECK: %val = load target("btype"), ptr %mem +; CHECK: ret target("btype") poison +; CHECK: } diff --git a/llvm/test/Transforms/GVN/target-type.ll b/llvm/test/Transforms/GVN/target-type.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/GVN/target-type.ll @@ -0,0 +1,52 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -passes=gvn < %s | FileCheck %s + +; Check that GVN can work with opaque types correctly. + +target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64" + +define target("type") @basic_alloc(target("type") %arg) { +; CHECK-LABEL: @basic_alloc( +; CHECK-NEXT: [[VAL:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: store target("type") [[ARG:%.*]], ptr [[VAL]], align 8 +; CHECK-NEXT: ret target("type") [[ARG]] +; + %val = alloca target("type") + store target("type") %arg, ptr %val + %ret = load target("type"), ptr %val + ret target("type") %ret +} + +define target("type") @nobitcast(ptr %arg) { +; CHECK-LABEL: @nobitcast( +; CHECK-NEXT: [[VAL:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: store ptr [[ARG:%.*]], ptr [[VAL]], align 8 +; CHECK-NEXT: [[RET:%.*]] = load target("type"), ptr [[VAL]], align 8 +; CHECK-NEXT: ret target("type") [[RET]] +; + %val = alloca target("type") + store ptr %arg, ptr %val + %ret = load target("type"), ptr %val + ret target("type") %ret +} + +define target("type") @viai64(target("type") %arg) { +; CHECK-LABEL: @viai64( +; CHECK-NEXT: [[VAL:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: [[BAR:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: store target("type") [[ARG:%.*]], ptr [[VAL]], align 8 +; CHECK-NEXT: [[IMEMCPY:%.*]] = load i64, ptr [[VAL]], align 4 +; CHECK-NEXT: store i64 [[IMEMCPY]], ptr [[BAR]], align 4 +; CHECK-NEXT: [[RET:%.*]] = load target("type"), ptr [[BAR]], align 8 +; CHECK-NEXT: ret target("type") [[RET]] +; + %val = alloca target("type") + %bar = alloca target("type") + store target("type") %arg, ptr %val + %imemcpy = load i64, ptr %val + store i64 %imemcpy, ptr %bar + %ret = load target("type"), ptr %bar + ret target("type") %ret +} + +declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1) diff --git a/llvm/test/Transforms/SROA/sroa-target.ll b/llvm/test/Transforms/SROA/sroa-target.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/SROA/sroa-target.ll @@ -0,0 +1,62 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -passes=sroa < %s | FileCheck %s + +; Check that SROA can work with opaque types correctly. + +target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64" + +define target("type") @basic_alloc(target("type") %arg) { +; CHECK-LABEL: @basic_alloc( +; CHECK-NEXT: ret target("type") [[ARG:%.*]] +; + %val = alloca target("type") + store target("type") %arg, ptr %val + %ret = load target("type"), ptr %val + ret target("type") %ret +} + +define target("type") @via_memcpy(target("type") %arg) { +; CHECK-LABEL: @via_memcpy( +; CHECK-NEXT: ret target("type") [[ARG:%.*]] +; + %val = alloca target("type") + %bar = alloca target("type") + store target("type") %arg, ptr %val + call void @llvm.memcpy.p0.p0.i64(ptr %bar, ptr %val, i64 8, i1 false) + %ret = load target("type"), ptr %bar + ret target("type") %ret +} + +define target("type") @nobitcast(ptr %arg) { +; CHECK-LABEL: @nobitcast( +; CHECK-NEXT: [[VAL:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: store ptr [[ARG:%.*]], ptr [[VAL]], align 8 +; CHECK-NEXT: [[VAL_0_RET:%.*]] = load target("type"), ptr [[VAL]], align 8 +; CHECK-NEXT: ret target("type") [[VAL_0_RET]] +; + %val = alloca target("type") + store ptr %arg, ptr %val + %ret = load target("type"), ptr %val + ret target("type") %ret +} + +define target("type") @viai64(target("type") %arg) { +; CHECK-LABEL: @viai64( +; CHECK-NEXT: [[VAL:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: [[BAR:%.*]] = alloca target("type"), align 8 +; CHECK-NEXT: store target("type") [[ARG:%.*]], ptr [[VAL]], align 8 +; CHECK-NEXT: [[VAL_0_IMEMCPY:%.*]] = load i64, ptr [[VAL]], align 8 +; CHECK-NEXT: store i64 [[VAL_0_IMEMCPY]], ptr [[BAR]], align 8 +; CHECK-NEXT: [[BAR_0_RET:%.*]] = load target("type"), ptr [[BAR]], align 8 +; CHECK-NEXT: ret target("type") [[BAR_0_RET]] +; + %val = alloca target("type") + %bar = alloca target("type") + store target("type") %arg, ptr %val + %imemcpy = load i64, ptr %val + store i64 %imemcpy, ptr %bar + %ret = load target("type"), ptr %bar + ret target("type") %ret +} + +declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1) diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -159,6 +159,8 @@ return LLVMX86MMXTypeInContext(Ctx); case LLVMTokenTypeKind: return LLVMTokenTypeInContext(Ctx); + case LLVMTargetExtTypeKind: + assert(false && "Implement me"); } fprintf(stderr, "%d is not a supported typekind\n", Kind); diff --git a/llvm/unittests/IR/TypesTest.cpp b/llvm/unittests/IR/TypesTest.cpp --- a/llvm/unittests/IR/TypesTest.cpp +++ b/llvm/unittests/IR/TypesTest.cpp @@ -61,6 +61,17 @@ EXPECT_FALSE(P2C0->isOpaque()); } +TEST(TypesTest, TargetExtType) { + LLVMContext Context; + Type *A = TargetExtType::get(Context, "typea"); + Type *Aparam = TargetExtType::get(Context, "typea", {}, {0, 1}); + Type *Aparam2 = TargetExtType::get(Context, "typea", {}, {0, 1}); + // Opaque types with same parameters are identical... + EXPECT_EQ(Aparam, Aparam2); + // ... but just having the same name is not enough. + EXPECT_NE(A, Aparam); +} + TEST(TypedPointerType, PrintTest) { std::string Buffer; LLVMContext Context;