@@ -912,9 +912,14 @@ static LogicalResult verify(AddressOfOp op) {
912912// / the name of the attribute in ODS.
913913static StringRef getLinkageAttrName () { return " linkage" ; }
914914
915+ // / Returns the name used for the TLS attribute. This *must* correspond to
916+ // / the name of the attribute in ODS.
917+ static StringRef getThreadLocalModeAttrName () { return " tls_mode" ; }
918+
919+
915920void GlobalOp::build (Builder *builder, OperationState &result, LLVMType type,
916- bool isConstant, Linkage linkage, StringRef name ,
917- Attribute value, unsigned addrSpace,
921+ bool isConstant, Linkage linkage, ThreadLocalMode tlsMode ,
922+ StringRef name, Attribute value, unsigned addrSpace,
918923 ArrayRef<NamedAttribute> attrs) {
919924 result.addAttribute (SymbolTable::getSymbolAttrName (),
920925 builder->getStringAttr (name));
@@ -925,6 +930,8 @@ void GlobalOp::build(Builder *builder, OperationState &result, LLVMType type,
925930 result.addAttribute (" value" , value);
926931 result.addAttribute (getLinkageAttrName (), builder->getI64IntegerAttr (
927932 static_cast <int64_t >(linkage)));
933+ result.addAttribute (getThreadLocalModeAttrName (), builder->getI64IntegerAttr (
934+ static_cast <int64_t >(tlsMode)));
928935 if (addrSpace != 0 )
929936 result.addAttribute (" addr_space" , builder->getI32IntegerAttr (addrSpace));
930937 result.attributes .append (attrs.begin (), attrs.end ());
@@ -933,6 +940,9 @@ void GlobalOp::build(Builder *builder, OperationState &result, LLVMType type,
933940
934941static void printGlobalOp (OpAsmPrinter &p, GlobalOp op) {
935942 p << op.getOperationName () << ' ' << stringifyLinkage (op.linkage ()) << ' ' ;
943+ auto tlsMode = op.tls_mode ();
944+ if (tlsMode != ThreadLocalMode::NotThreadLocal)
945+ p << stringifyThreadLocalMode (tlsMode) << ' ' ;
936946 if (op.constant ())
937947 p << " constant " ;
938948 p.printSymbolName (op.sym_name ());
@@ -942,7 +952,7 @@ static void printGlobalOp(OpAsmPrinter &p, GlobalOp op) {
942952 p << ' )' ;
943953 p.printOptionalAttrDict (op.getAttrs (),
944954 {SymbolTable::getSymbolAttrName (), " type" , " constant" ,
945- " value" , getLinkageAttrName ()});
955+ " value" , getLinkageAttrName (), getThreadLocalModeAttrName () });
946956
947957 // Print the trailing type unless it's a string global.
948958 if (op.getValueOrNull ().dyn_cast_or_null <StringAttr>())
@@ -976,14 +986,15 @@ template <typename Ty> struct EnumTraits {};
976986 }
977987
978988REGISTER_ENUM_TYPE (Linkage);
989+ REGISTER_ENUM_TYPE (ThreadLocalMode);
979990} // end namespace
980991
981992template <typename EnumTy>
982993static ParseResult parseOptionalLLVMKeyword (OpAsmParser &parser,
983994 OperationState &result,
984995 StringRef name) {
985996 SmallVector<StringRef, 10 > names;
986- for (unsigned i = 0 , e = getMaxEnumValForLinkage (); i <= e; ++i)
997+ for (unsigned i = 0 , e = EnumTraits<EnumTy>:: getMaxEnumVal (); i <= e; ++i)
987998 names.push_back (EnumTraits<EnumTy>::stringify (static_cast <EnumTy>(i)));
988999
9891000 int index = parseOptionalKeywordAlternative (parser, names);
@@ -993,7 +1004,7 @@ static ParseResult parseOptionalLLVMKeyword(OpAsmParser &parser,
9931004 return success ();
9941005}
9951006
996- // operation ::= `llvm.mlir.global` linkage `constant`? `@` identifier
1007+ // operation ::= `llvm.mlir.global` linkage tls-mode? `constant`? `@` identifier
9971008// `(` attribute? `)` attribute-list? (`:` type)? region?
9981009//
9991010// The type can be omitted for string attributes, in which case it will be
@@ -1003,6 +1014,8 @@ static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
10031014 getLinkageAttrName ())))
10041015 return parser.emitError (parser.getCurrentLocation (), " expected linkage" );
10051016
1017+ parseOptionalLLVMKeyword<ThreadLocalMode>(parser, result, getThreadLocalModeAttrName ());
1018+
10061019 if (succeeded (parser.parseOptionalKeyword (" constant" )))
10071020 result.addAttribute (" constant" , parser.getBuilder ().getUnitAttr ());
10081021
0 commit comments